forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabin_miller.py
More file actions
84 lines (61 loc) · 1.86 KB
/
rabin_miller.py
File metadata and controls
84 lines (61 loc) · 1.86 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Rabin-Miller Primality Test
A probabilistic primality test where returning False guarantees the number
is composite, and returning True means the number is probably prime with
a 4^(-k) chance of error.
Reference: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
Complexity:
Time: O(k * log^2(n))
Space: O(1)
"""
from __future__ import annotations
import random
def is_prime(n: int, k: int) -> bool:
"""Test if n is probably prime using the Rabin-Miller algorithm.
Args:
n: The integer to test for primality.
k: The number of rounds of testing (higher = more accurate).
Returns:
True if n is probably prime, False if n is definitely composite.
Examples:
>>> is_prime(7, 2)
True
>>> is_prime(6, 2)
False
"""
def _pow2_factor(num: int) -> tuple[int, float]:
"""Factor num into 2^power * odd_part.
Args:
num: The integer to factor.
Returns:
A tuple (power, odd_part).
"""
power = 0
while num % 2 == 0:
num /= 2
power += 1
return power, num
def _valid_witness(a: int) -> bool:
"""Check if a is a witness for the compositeness of n.
Args:
a: The potential witness value.
Returns:
True if a proves n is composite, False otherwise.
"""
x = pow(int(a), int(d), int(n))
if x == 1 or x == n - 1:
return False
for _ in range(r - 1):
x = pow(int(x), 2, int(n))
if x == 1:
return True
if x == n - 1:
return False
return True
if n < 5:
return n == 2 or n == 3
r, d = _pow2_factor(n - 1)
return all(
not _valid_witness(random.randrange(2, n - 2))
for _ in range(k)
)