-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path58. All primitive roots modulo 22.py
54 lines (40 loc) · 1.15 KB
/
58. All primitive roots modulo 22.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def is_primitive_root_mod_n(a, n):
"""
Checks if a is a primitive root modulo n.
Args:
a: The number to check.
n: The modulus.
Returns:
True if a is a primitive root modulo n, False otherwise.
"""
# Check if a is coprime to n.
if math.gcd(a, n) != 1:
return False
# Iterate from 1 to n - 1.
for i in range(1, n):
# If a ** i is 1 modulo n, then a is not a primitive root modulo n.
if (a ** i) % n == 1:
return False
# a is a primitive root modulo n.
return True
def all_primitive_roots_mod_n(n):
"""
Calculates all primitive roots modulo n.
Args:
n: The modulus.
Returns:
A list of all primitive roots modulo n.
"""
# Initialize the list of primitive roots.
primitive_roots = []
# Iterate from 2 to n-1.
for a in range(2, n):
# If a is a primitive root modulo n, then add it to the list.
if is_primitive_root_mod_n(a, n):
primitive_roots.append(a)
# Return the list of primitive roots.
return primitive_roots
if __name__ == "__main__":
n = 22
primitive_roots = all_primitive_roots_mod_n(n)
print("The primitive roots modulo {} are {}".format(n, primitive_roots))