-
Notifications
You must be signed in to change notification settings - Fork 2
/
prime_test.py
50 lines (42 loc) · 1.23 KB
/
prime_test.py
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
"""
Final program which combines Atkin-Morain ECPP with Miller-Rabin Primality test.
"""
from miller_rabin import miller_rabin
from ecpp import atkin_morain
import sys
def prime(n):
"""
Final Product: Run Miller-Rabin first, use ECPP only when its probable prime.
Args:
n: Number to be tested
Returns:
certificate if the number is prime, False otherwise.
"""
if not n > 0:
raise ValueError("input must be greater than 0")
repeat = 50
if miller_rabin(n, repeat):
cert = atkin_morain(n)
if not cert:
raise InconsistencyError("ECPP")
else:
return cert
else:
return False
class InconsistencyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self)
if __name__=='__main__':
n = int(sys.argv[1])
try:
cert = prime(n)
if not cert:
print str(n) + " is composite."
else:
print str(n) + " is prime and the certificate is " + str(cert)
except ValueError:
print "Input must be greater than 0."
except InconsistencyError:
print "It's a probable prime yet no certificate was found. Please try again."