forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiller_rabin_method.py
More file actions
56 lines (48 loc) · 1.55 KB
/
Copy pathmiller_rabin_method.py
File metadata and controls
56 lines (48 loc) · 1.55 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
import random
def miller_rabin_method(number: int, rounds: int = 40) -> bool:
"""Miller-Rabin primality test
Test if number could be prime using the Miller-Rabin Primality Test with rounds rounds.
A return value of false means number is definitely composite, while true means it is probably prime.
The higher rounds is, the more accurate the test is.
:param int number: The number to be tested for primality.
:param int rounds: How many rounds to use in the test.
:return: A bool indicating if the number could be prime or not.
:rtype: bool
"""
# Handle corner cases
if number == 1:
return False
if number == 2:
return True
if number == 3:
return True
# Factor out the powers of 2 from {number - 1} and save the result
d = number - 1
r = 0
while not d & 1:
d = d >> 1
r += 1
# Cycle at most {round} times
for i in range(rounds + 1):
a = random.randint(2, number - 2)
x = pow(a, d, number)
if x == 1 or x == number - 1:
continue
# Cycle at most {r - 1} times
for e in range(r):
x = x * x % number
if x == number - 1:
break
if x == number - 1:
continue
return False
return True
if __name__ == "__main__":
count = 0
upper_bound = 1000
print(f"Prime numbers lower than {upper_bound}:")
for i in range(1, 1000):
if miller_rabin_method(i):
print(f"\t{i}")
count += 1
print(f"Total: {count}")