Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Math 446 Spring 2019

Project: Math 446
Views: 156
help()

Welcome to Sage 8.4!

  • Online documentation: View the Sage documentation online.

  • Help: For help on any object or function, for example matrix_plot, enter matrix_plot? followed by tab or shift+enter. For help on any module (or object or function), for example, sage.matrix, enter help(sage.matrix).

  • Tab completion: Type obj followed by tab to see all completions of obj. To see all methods you may call on obj, type obj. followed by tab.

  • Source code: Enter matrix_plot?? followed by tab or shift+enter to look at the source code of matrix_plot.

  • License information: For license information about Sage and its components, enter license().

is_prime?
File: /ext/sage/sage-8.4_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)
# test if a number is prime is_prime(5)
True
#arithemtic print 2^3 + 2 * 5 - 3 print "2^(2^3) equals",2^(2^3)
15 2^(2^3) equals 256
#list of primes list = prime_range(100) print "There are",len(list),"primes less than 100"
There are 25 primes less than 100
#a way to access things in a list for n in list: if n% 4 == 1: print n
5 13 17 29 37 41 53 61 73 89 97
# building a new list list2 = [ p^2 for p in list] list2
[4, 9, 25, 49, 121, 169, 289, 361, 529, 841, 961, 1369, 1681, 1849, 2209, 2809, 3481, 3721, 4489, 5041, 5329, 6241, 6889, 7921, 9409]
#how many times does 7921 appear in list2? list2.count(7921)
1
#do arithmetic modulo n #find an inverse of 5 moduloe 7 Mod(5,7)^(-1)
3
R = Integers(7) for a in R: print a,a^2
0 0 1 1 2 4 3 2 4 2 5 4 6 1
#Solving Linear Diophantine Equations #xgcd returns gcd(a,b), plus x and y that solve ax+by = gcd(a,b) xgcd(5,7)
(1, 3, -2)
5*3 + 7 * (-2)
1
#x \equiv a \pmod{n} and x \equiv b \pmod{m} #crt(a,b,n,m) crt(5,6,11,13)
71
#squares modulo n is_square(Mod(2^2,101))
True
is_square(Mod(2,101))
False
#primitive roots and order Mod(2,101).multiplicative_order()
100
primitive_root(101)
2
def encode(s): s = str(s) return sum(ord(s[i])*256^i for i in range(len(s))) def decode(n): n = Integer(n) v= [] while n!=0 : v.append(chr(n %256)) n//= 256 # replaces n by floor(n/256) return ''.join(v)
m = encode('I like number theory') m
693339872193417233970224178043240443845515419721
decode(m)
'I like number theory'
#large powers, efficiently Mod(3,10003)^10454838383
341
#Gaussian Integers ZZ[I]
Order in Number Field in I with defining polynomial x^2 + 1
a = ZZ[I](5) factor(a)
(I) * (-I - 2) * (2*I + 1)
(2 + I) *(3-I)
I + 7
continued_fraction(e)
[2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, ...]
cf = continued_fraction([2,5,4,3,2,7]) cf.convergents()
[2, 11/5, 46/21, 149/68, 344/157, 2557/1167]