-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathmiller_rabin_primality test.py
More file actions
54 lines (44 loc) · 1.07 KB
/
miller_rabin_primality test.py
File metadata and controls
54 lines (44 loc) · 1.07 KB
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
import random
def miller_rabin_primality_test(n: int, k: int = 5) -> bool:
"""
Probabilistic primality test using Miller-Rabin algorithm.
Args:
n (int): Number to test for primality.
k (int): Number of iterations for accuracy.
Returns:
bool: True if n is probably prime, False if composite.
Examples:
>>> miller_rabin_primality_test(2)
True
>>> miller_rabin_primality_test(15)
False
>>> miller_rabin_primality_test(17)
True
"""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0:
return False
# Write n-1 as 2^r * d
d = n - 1
r = 0
while d % 2 == 0:
d //= 2
r += 1
for _ in range(k):
a = random.randint(2, n - 2)
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True
if __name__ == "__main__":
import doctest
doctest.testmod()