SharedSAGE1.sagewsOpen in CoCalc
Author: Ncho De Gregorio
Views : 27
Description: Practica
#EJERCICIO SAGE1 is_prime?
File: /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/arith/misc.py Signature : is_prime(n) Docstring : Return "True" if n is a prime number, and "False" otherwise. Use a provable primality test or a strong pseudo-primality test depending on the global "arithmetic proof flag". INPUT: * "n" - the object for which to determine primality See also: * "is_pseudoprime()" * "sage.rings.integer.Integer.is_prime()" AUTHORS: * Kevin Stueve kstueve@uw.edu (2010-01-17): delegated calculation to "n.is_prime()" EXAMPLES: sage: is_prime(389) True sage: is_prime(2000) False sage: is_prime(2) True sage: is_prime(-1) False sage: is_prime(1) False sage: is_prime(-2) False sage: a = 2**2048 + 981 sage: is_prime(a) # not tested - takes ~ 1min sage: proof.arithmetic(False) sage: is_prime(a) # instantaneous! True sage: proof.arithmetic(True)
is_prime(7)
True
factor?
File: /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/arith/misc.py Signature : factor(n, proof=None, int_=False, algorithm='pari', verbose=0, **kwds) Docstring : Returns the factorization of "n". The result depends on the type of "n". If "n" is an integer, returns the factorization as an object of type "Factorization". If n is not an integer, "n.factor(proof=proof, **kwds)" gets called. See "n.factor??" for more documentation in this case. Warning: This means that applying "factor" to an integer result of a symbolic computation will not factor the integer, because it is considered as an element of a larger symbolic ring.EXAMPLES: sage: f(n)=n^2 sage: is_prime(f(3)) False sage: factor(f(3)) 9 INPUT: * "n" - an nonzero integer * "proof" - bool or None (default: None) * "int_" - bool (default: False) whether to return answers as Python ints * "algorithm" - string * "'pari'" - (default) use the PARI c library * "'kash'" - use KASH computer algebra system (requires the optional kash package be installed) * "'magma'" - use Magma (requires magma be installed) * "verbose" - integer (default: 0); PARI's debug variable is set to this; e.g., set to 4 or 8 to see lots of output during factorization. OUTPUT: * factorization of n The qsieve and ecm commands give access to highly optimized implementations of algorithms for doing certain integer factorization problems. These implementations are not used by the generic factor command, which currently just calls PARI (note that PARI also implements sieve and ecm algorithms, but they aren't as optimized). Thus you might consider using them instead for certain numbers. The factorization returned is an element of the class "Factorization"; see Factorization?? for more details, and examples below for usage. A Factorization contains both the unit factor (+1 or -1) and a sorted list of (prime, exponent) pairs. The factorization displays in pretty-print format but it is easy to obtain access to the (prime,exponent) pairs and the unit, to recover the number from its factorization, and even to multiply two factorizations. See examples below. EXAMPLES: sage: factor(500) 2^2 * 5^3 sage: factor(-20) -1 * 2^2 * 5 sage: f=factor(-20) sage: list(f) [(2, 2), (5, 1)] sage: f.unit() -1 sage: f.value() -20 sage: factor( -next_prime(10^2) * next_prime(10^7) ) -1 * 101 * 10000019 sage: factor(-500, algorithm='kash') # optional - kash -1 * 2^2 * 5^3 sage: factor(-500, algorithm='magma') # optional - magma -1 * 2^2 * 5^3 sage: factor(0) Traceback (most recent call last): ... ArithmeticError: factorization of 0 is not defined sage: factor(1) 1 sage: factor(-1) -1 sage: factor(2^(2^7)+1) 59649589127497217 * 5704689200685129054721 Sage calls PARI's factor, which has proof False by default. Sage has a global proof flag, set to True by default (see "sage.structure.proof.proof", or proof.[tab]). To override the default, call this function with proof=False. sage: factor(3^89-1, proof=False) 2 * 179 * 1611479891519807 * 5042939439565996049162197 sage: factor(2^197 + 1) # long time (2s) 3 * 197002597249 * 1348959352853811313 * 251951573867253012259144010843 Any object which has a factor method can be factored like this: sage: K.<i> = QuadraticField(-1) sage: factor(122 - 454*i) (-3*i - 2) * (-i - 2)^3 * (i + 1)^3 * (i + 4) To access the data in a factorization: sage: f = factor(420); f 2^2 * 3 * 5 * 7 sage: [x for x in f] [(2, 2), (3, 1), (5, 1), (7, 1)] sage: [p for p,e in f] [2, 3, 5, 7] sage: [e for p,e in f] [2, 1, 1, 1] sage: [p^e for p,e in f] [4, 3, 5, 7] We can factor Python and numpy numbers: sage: factor(math.pi) 3.141592653589793 sage: import numpy sage: factor(numpy.int8(30)) 2 * 3 * 5
factor(73652)
2^2 * 18413
#EJERCICIO SAGE2 primes?
File: /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/arith/misc.py Signature : primes(start, stop=None, proof=None) Docstring : Returns an iterator over all primes between start and stop-1, inclusive. This is much slower than "prime_range", but potentially uses less memory. As with "next_prime()", the optional argument proof controls whether the numbers returned are guaranteed to be prime or not. This command is like the Python 2 "xrange" command, except it only iterates over primes. In some cases it is better to use primes than "prime_range", because primes does not build a list of all primes in the range in memory all at once. However, it is potentially much slower since it simply calls the "next_prime()" function repeatedly, and "next_prime()" is slow. INPUT: * "start" - an integer - lower bound for the primes * "stop" - an integer (or infinity) optional argument - giving upper (open) bound for the primes * "proof" - bool or None (default: None) If True, the function yields only proven primes. If False, the function uses a pseudo- primality test, which is much faster for really big numbers but does not provide a proof of primality. If None, uses the global default (see "sage.structure.proof.proof") OUTPUT: * an iterator over primes from start to stop-1, inclusive EXAMPLES: sage: for p in primes(5,10): ....: print(p) 5 7 sage: list(primes(13)) [2, 3, 5, 7, 11] sage: list(primes(10000000000, 10000000100)) [10000000019, 10000000033, 10000000061, 10000000069, 10000000097] sage: max(primes(10^100, 10^100+10^4, proof=False)) 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009631 sage: next(p for p in primes(10^20, infinity) if is_prime(2*p+1)) 100000000000000001243
len(list(primes(1,10**2)))
25
list(primes(1,100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
len?
File: Docstring : len(object) -> integer Return the number of items of a sequence or collection.
f=(abs(len(list(primes(1,x)))))/(x/ln(x))0 RDF(f, x = +oo)
Error in lines 2-2 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags) in namespace, locals File "", line 1, in <module> File "sage/structure/parent.pyx", line 861, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9403) def __call__(self, x=0, *args, **kwds): TypeError: __call__() got multiple values for keyword argument 'x'
gcd(636,163)
1
gcd(1,0)
1
lcm(1,0)
0
3874%1129
487
gcd(3,0)
3
(93585*40407)/3
1260496365
636*163
103668
RDF?
File: /ext/sage/sage-8.3_1804/src/sage/rings/real_double.pyx Signature : RDF() Docstring : An approximation to the field of real numbers using double precision floating point numbers. Answers derived from calculations in this approximation may differ from what they would be if those calculations were performed in the true field of real numbers. This is due to the rounding errors inherent to finite precision calculations. EXAMPLES: sage: RR == RDF False sage: RDF == RealDoubleField() # RDF is the shorthand True sage: RDF(1) 1.0 sage: RDF(2/3) 0.6666666666666666 A "TypeError" is raised if the coercion doesn't make sense: sage: RDF(QQ['x'].0) Traceback (most recent call last): ... TypeError: cannot coerce nonconstant polynomial to float sage: RDF(QQ['x'](3)) 3.0 One can convert back and forth between double precision real numbers and higher-precision ones, though of course there may be loss of precision: sage: a = RealField(200)(2).sqrt(); a 1.4142135623730950488016887242096980785696718753769480731767 sage: b = RDF(a); b 1.4142135623730951 sage: a.parent()(b) 1.4142135623730951454746218587388284504413604736328125000000 sage: a.parent()(b) == b True sage: b == RR(a) True
x f=(abs(len(list(primes(1,x)))))/(x/ln(x)) limit(f, x = oo)
0
q = len(list(primes(1,10000)))) p = numerical_approx(10000/ln(10000)) p q
Error in lines 0-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags) in namespace, locals File "<string>", line 1 q = len(list(primes(Integer(1),Integer(10000))))) ^ SyntaxError: invalid syntax
3853%2018
1835
2018%1835
183
1835%183
5
354%212
142
142%19
9
gcd(26,15)
1
26%15
11
15%11 11%4
4 3
4%3
1
3%1
0
1%3
1
26*(-1) + 15*2
4
d,u,v = xgcd(26,15) 106*d == u*26 + v*15
False
xgcd?
File: /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/arith/misc.py Signature : xgcd(a, b) Docstring : Return a triple "(g,s,t)" such that g = s * a+t * b = gcd(a,b). Note: One exception is if a and b are not in a principal ideal domain (see https://en.wikipedia.org/wiki/Principal_ideal_domain), e.g., they are both polynomials over the integers. Then this function can't in general return "(g,s,t)" as above, since they need not exist. Instead, over the integers, we first multiply g by a divisor of the resultant of a/g and b/g, up to sign. INPUT: * "a, b" - integers or more generally, element of a ring for which the xgcd make sense (e.g. a field or univariate polynomials). OUTPUT: * "g, s, t" - such that g = s * a + t * b Note: There is no guarantee that the returned cofactors (s and t) are minimal. EXAMPLES: sage: xgcd(56, 44) (4, 4, -5) sage: 4*56 + (-5)*44 4 sage: g, a, b = xgcd(5/1, 7/1); g, a, b (1, 3, -2) sage: a*(5/1) + b*(7/1) == g True sage: x = polygen(QQ) sage: xgcd(x^3 - 1, x^2 - 1) (x - 1, 1, -x) sage: K.<g> = NumberField(x^2-3) sage: g.xgcd(g+2) (1, 1/3*g, 0) sage: R.<a,b> = K[] sage: S.<y> = R.fraction_field()[] sage: xgcd(y^2, a*y+b) (1, a^2/b^2, ((-a)/b^2)*y + 1/b) sage: xgcd((b+g)*y^2, (a+g)*y+b) (1, (a^2 + (2*g)*a + 3)/(b^3 + (g)*b^2), ((-a + (-g))/b^2)*y + 1/b) Here is an example of a xgcd for two polynomials over the integers, where the linear combination is not the gcd but the gcd multiplied by the resultant: sage: R.<x> = ZZ[] sage: gcd(2*x*(x-1), x^2) x sage: xgcd(2*x*(x-1), x^2) (2*x, -1, 2) sage: (2*(x-1)).resultant(x) 2
826%621
205
621%205
6
621//205
3
205//6 205%6
34 1
552%6 552//6
0 92
368%92 368//92
0 4
552//368 552%368
1 184
368//164 368%164
2 40
184//40 184%40
4 24
40//24 40%24
1 16
828%368 621%368 552%368
92 253 184
(14^12)%19 368%92 253%92 184%92
11 0 69 0
92%69
23
69%23
0
3^4%39 27354//18
3 1519
(3*9*33)%68
7
Mod(3^47,189)
54
(243)%68
39
n=337 x=4 exp = x^n mod= exp%n if mod == x: print 'pasa.' if mod != x: print 'No pasa.'
pasa.