forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_order_simple.py
More file actions
43 lines (33 loc) · 882 Bytes
/
find_order_simple.py
File metadata and controls
43 lines (33 loc) · 882 Bytes
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
"""
Multiplicative Order
Find the multiplicative order of a modulo n, which is the smallest positive
integer k such that a^k = 1 (mod n). Requires gcd(a, n) = 1.
Reference: https://en.wikipedia.org/wiki/Multiplicative_order
Complexity:
Time: O(n log n)
Space: O(1)
"""
from __future__ import annotations
import math
def find_order(a: int, n: int) -> int:
"""Find the multiplicative order of a modulo n.
Args:
a: The base integer.
n: The modulus.
Returns:
The smallest positive k where a^k = 1 (mod n), or -1 if a and n
are not coprime.
Examples:
>>> find_order(3, 7)
6
>>> find_order(1, 1)
1
"""
if (a == 1) & (n == 1):
return 1
if math.gcd(a, n) != 1:
return -1
for i in range(1, n):
if pow(a, i) % n == 1:
return i
return -1