-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63. Products of primitive roots modulo p.py
74 lines (54 loc) · 1.8 KB
/
63. Products of primitive roots modulo p.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def products_of_primitive_roots_mod_p(p):
"""
Finds the products of all pairs of primitive roots modulo p.
Args:
p: The modulus.
Returns:
A list of the products of all pairs of primitive roots modulo p.
"""
# Get the list of primitive roots modulo p.
primitive_roots_mod_p = primitive_roots_mod_p(p)
# Initialize a list to store the products of all pairs of primitive roots modulo p.
products_of_primitive_roots_mod_p = []
# Iterate over all pairs of primitive roots modulo p.
for i in range(len(primitive_roots_mod_p)):
for j in range(i + 1, len(primitive_roots_mod_p)):
products_of_primitive_roots_mod_p.append(primitive_roots_mod_p[i] * primitive_roots_mod_p[j])
return products_of_primitive_roots_mod_p
def primitive_roots_mod_p(p):
"""
Finds all primitive roots modulo p.
Args:
p: The modulus.
Returns:
A list of primitive roots modulo p.
"""
# Create a list to store the primitive roots modulo p.
primitive_roots_mod_p = []
# Iterate over all integers from 2 to p - 1.
for i in range(2, p):
# If the integer is a primitive root modulo p, add it to the list.
if is_primitive_root(i, p):
primitive_roots_mod_p.append(i)
return primitive_roots_mod_p
def is_primitive_root(a, p):
"""
Checks if the integer a is a primitive root modulo p.
Args:
a: The integer.
p: The modulus.
Returns:
True if the integer a is a primitive root modulo p, False otherwise.
"""
# Check if a is an integer greater than 1 and less than p.
if a <= 1 or a >= p:
return False
# Check if the order of a modulo p is p - 1.
order_of_a = order_of_integer(a, p)
if order_of_a != p - 1:
return False
# Check if a is a primitive root modulo p.
for i in range(2, p):
if pow(a, i) % p == 1:
return False
return True