MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / find_primitive

Function find_primitive

ciphers/diffie.py:4–31  ·  view source on GitHub ↗

Find a primitive root modulo modulus, if one exists. Args: modulus : The modulus for which to find a primitive root. Returns: The primitive root if one exists, or None if there is none. Examples: >>> find_primitive(7) # Modulo 7 has primitive root 3

(modulus: int)

Source from the content-addressed store, hash-verified

2
3
4def find_primitive(modulus: int) -> int | None:
5 """
6 Find a primitive root modulo modulus, if one exists.
7
8 Args:
9 modulus : The modulus for which to find a primitive root.
10
11 Returns:
12 The primitive root if one exists, or None if there is none.
13
14 Examples:
15 >>> find_primitive(7) # Modulo 7 has primitive root 3
16 3
17 >>> find_primitive(11) # Modulo 11 has primitive root 2
18 2
19 >>> find_primitive(8) == None # Modulo 8 has no primitive root
20 True
21 """
22 for r in range(1, modulus):
23 li = []
24 for x in range(modulus - 1):
25 val = pow(r, x, modulus)
26 if val in li:
27 break
28 li.append(val)
29 else:
30 return r
31 return None
32
33
34if __name__ == "__main__":

Callers 1

diffie.pyFile · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected