Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Congruence arithmetic examples

73 views
ubuntu2004
######################################### # Congruence arithmetic - basics; # see also http://userpages.umbc.edu/~rcampbel/Computers/SAGE/numbthy.html # and http://www.math.ucla.edu/~jimc/mathnet_d/sage/reference/sage/rings/finite_rings/integer_mod_ring.html # or http://doc.sagemath.org/html/en/reference/rings_standard/sage/rings/finite_rings/integer_mod_ring.html ######################################### # Part I # Defining the ring Z/NZ R=Zmod(20); R # Z/20Z R.order() R.list()
Ring of integers modulo 20 20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# Generators R.gens() R.gen(0) # Why not all Z/nZ*?
(1,)
File: /ext/sage/sage-8.3_1804/src/sage/rings/finite_rings/integer_mod.pyx Docstring : Elements of ZZ/nZZ for n small enough to be operated on in 32 bits AUTHORS: * Robert Bradshaw (2006-08-24) EXAMPLES: sage: a = Mod(10,30); a 10 sage: loads(a.dumps()) == a True
# Specifying elements x=R(5); print "Converting the integer 5 into 5 mod 20=", x a=Mod(7,20); a # a modulo n as element of Z/nZ: Mod(a, n)
Converting the integer 5 into 5 mod 20= 5 7
# Addition a+ 19 # 7+15 mod 20=2
6
# Multiplicative group for R=Z/20Z U=R.list_of_elements_of_multiplicative_group() # The group of units U=(Z/nZ)* print "Z/20Z*= ", U, " has ", len(U), " elements" U[0] # is always 1 # Accesing individual units, e.g. the 3rd unit U[2] G=R.unit_gens(); G # multiplicative generators, i.e. Primitive roots print "No. of generators (rank/dimension):", len(G) m=1 for k in range(len(G)): o=multiplicative_order(G[k]) print "gen.", k, "is ", G[k], " additive order is ", G[k].order(), " mult. order:", o m=m*o # print m, len(G), len(U) if m==len(U): print "No of elements", len(U), " equals prod. of orders of gen.", m # How to access the group structure?
Z/20Z*= [1, 3, 7, 9, 11, 13, 17, 19] has 8 elements 1 7 (11, 17) No. of generators (rank/dimension): 2 gen. 0 is 11 additive order is 20 mult. order: 2 gen. 1 is 17 additive order is 20 mult. order: 4 No of elements 8 equals prod. of orders of gen. 8
# The multiplicative orbit of generators for (Z/20Z)* for i in range(len(G)): g=G[i]; [g^k for k in range(g.multiplicative_order())]
[1, 11] [1, 17, 9, 13]
# Multiplication in Z/nZ R=IntegerModRing(20) # also denoted Zmod(20) or Integers(20) if R==Integers(20): print "IntegerModRing(n) == Integers(n)" # It is a list of 20 elements R.list() # Defining an element R(5) # converting integer 5 to 5 mod 20; NOT R[5] ... # ... but labeling the list allows to access it: L=R.list(); print L[0], L[7] # L[20] is not defined!
IntegerModRing(n) == Integers(n) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 5 0 7
# Multiplication mod 20 a=21 % 20; b=7 % 20; c=a*b # multiplication mod 20 print a,b,c if c==R(7): print "TRUE" # equal as elements in Z/20Z # inverse of n (mod m): n.inverse mod(m) # power a # n (mod m): power mod(a, n, m)
1 7 7 TRUE
# square root of a (mod n) =Mod(a,n).sqrt() Mod(1,7).sqrt() Mod(6,7).sqrt() # 6, i.e. -1, is not a quadratic residue mod 7 ... see below
1 sqrt6
# ************************************************************************************** # Part II: More Number Theory - functions & multiplication # From http://wiki.sagemath.org/quickref?action=AttachFile&do=get&target=quickref-nt.pdf # # Ring Z/nZ = Zmod(n) = IntegerModRing(n) # Euler’s φ(n) function: euler_phi(6) # equals the number of elements of multiplicative group: |Z/nZ|* Zmod(6).list_of_elements_of_multiplicative_group()
2 [1, 5]
# Kronecker symbol kronecker_symbol(a,b) kronecker_symbol(2,7) # QR Mod(2,7).sqrt() # sqrt is 3 print "Is 3^2=2? Yes: ", Mod(2,7).sqrt()^2 # checking 3^2=2 kronecker_symbol(6,7) # not a QR print "has no square root: ", Mod(6,7).sqrt() # Quadratic residues: quadratic_residues(7) # Quadratic non-residues: [k for k in range(7) if kronecker(k,7)==-1]
1 3 Is 3^2=2? Yes: 2 -1 has no square root: sqrt6 [0, 1, 2, 4] [3, 5, 6]
# primitive root modulo n g=primitive_root(7); g g.multiplicative_order() # To make g an element of Z/7Z g1=mod(g,7); g1 o=g1.multiplicative_order(); o # so ... order of a (mod n) =Mod(a,n).multiplicative order() # checking that it generates Z/7Z* ... [3^k % 7 for k in range(o)]
3 +Infinity 3 6 [1, 3, 2, 6, 4, 5]
# discrete log: log(Mod(6,7), Mod(3,7)) b=Mod(3,7); a=Mod(6,7); x=log(Mod(6,7), Mod(3,7)) print "a=",a," b=",b, " x=",x, "b^x=a i.e. x=log_b(a): ", b^x
a= 6 b= 3 x= 3 b^x=a i.e. x=log_b(a): 6
# Chinese remainder theorem: x = crt(a,b,m,n) # finds x with x ≡ a (mod m) and x ≡ b (mod n) a=1; m=2; b=3; n=5 print a,m,b,n x=crt(a,b,m,n); x
1 2 3 5 3
# Checking x % m # is a=1 x % n # is b=3
1 3