We can use Sage to do modular arithmetic and work in finite fields! Here are some commands that we'll need in this course.
We can use the Mod command in Sage to do modular arithmetic. To compute a (mod N) write Mod(a,N).
The following code computes g^a (mod N) for all a between 1 and N-1 and for any g in the set Z/NZ. It prints two columns: one for the values of a and one for the values of g^a (mod N). You can change N to be any positive integer!
N=7
g=4
for a in [1..N-1]:
print(a, Mod(g^a,N))
The following code computes g^a (mod N) for all a between 1 and N-1 and for all g in the set Z/NZ. It stores the data in lists and prints two things: the value of g and a list of the values of g^a (mod N).
N=7
for g in [1..N-1]:
powers=[]
for a in [1..N-1]:
powers.append(Mod(g^a,N))
print(g, powers)
In class we talked about primitive roots of the field Fp. The following code does a few things. First, euler_phi(n) computes phi(n), which tells us how many elements in Z/nZ are relatively prime to n. Second, phi(p-1) tells us how many primitive roots there are in Fp. You can change the prime p below.
p=13
euler_phi(p)
euler_phi(p-1)
Here's some more useful code for working in the field Fp!
The code below gives one primitive root in Fp.
p=13
primitive_root(p)
This code computes orders in Fp. Edit the code below to compute orders for different elements in Fp. Can you write code that will compute the orders of all of the elements and then list the primitive roots?
p=13
a=2
Mod(a,p).multiplicative_order()