-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJacob.py
72 lines (57 loc) · 1.91 KB
/
Jacob.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import math
import random
def Fermat(a,N):
if a >= 2 and a <= N-1 :
if a**(N - 1) % N != 1 % N :
print("N is composite")
else:
print("N is Prime")
else:
print("Nope!")
def Jacob(m,n):
if m == 1 :
return 1
elif m % 2 == 0 :
if (((n**2 - 1) / 8) % 2) == 0 :
return Jacob(m/2,n)
else :
return -Jacob(m/2,n)
elif (((m-1) * (n-1) / 4) % 2) == 0 :
return Jacob(n % m,m)
else :
return -Jacob(n % m,m)
def solovay_strassen(n, k=10):
if n == 2 or n == 3:
return True
if not n & 1:
return False
for i in range(k):
a = random.randrange(2, n - 1) # choose any random number from 1 to (n-1) 5
x = Jacob(a, n) # find n's jacobi number
y = pow(a, int((n - 1) / 2), n) # calculate legendre symbol from euler criterion formula
if y != 1 and y != 0:
y = -1
if (x == 0) or (y != x): # if jecobi and eular criterion formula are not same (y != x) then the number is not prime
return False
return True
def FermatCom(N):
x = math.ceil(math.sqrt(N))
n = 0
y = 0
while (y != math.ceil(y) and y != math.floor(y) or y == 0):
y = math.sqrt((x**2) - N)
x = x + 1
print(y)
x = x - 1
print(x)
n = (x + y) * (x - y)
print(n)
print(Jacob(2,77))
n = int(input('\nEnter number : ')) # enter number for check it's primality
if n <= 1 :
print('Number must be >=2')
elif(solovay_strassen(n,10)):
print (str(n) + ' is Prime!')
else:
print (str(n) + ' is not Prime!')
FermatCom(15770708441)