forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolovayStrassenMethod.py
101 lines (73 loc) · 2.04 KB
/
solovayStrassenMethod.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Python program to implement Solovay-Strassen Primality Test
import random
# modulo function to perform binary exponentiation
def modulo(base, exponent, mod):
x = 1;
y = base;
while (exponent > 0):
if (exponent % 2 == 1):
x = (x * y) % mod;
y = (y * y) % mod;
exponent = exponent // 2;
return x % mod;
# To calculate Jacobian symbol of a given number
def calculateJacobian(a, n):
if (a == 0):
return 0; # (0/n) = 0
ans = 1;
if (a < 0):
# (a/n) = (-a/n)*(-1/n)
a = -a;
if (n % 4 == 3):
# (-1/n) = -1 if n = 3 (mod 4)
ans = -ans;
if (a == 1):
return ans; # (1/n) = 1
while (a):
if (a < 0):
# (a/n) = (-a/n)*(-1/n)
a = -a;
if (n % 4 == 3):
# (-1/n) = -1 if n = 3 (mod 4)
ans = -ans;
while (a % 2 == 0):
a = a // 2;
if (n % 8 == 3 or n % 8 == 5):
ans = -ans;
# swap
a, n = n, a;
if (a % 4 == 3 and n % 4 == 3):
ans = -ans;
a = a % n;
if (a > n // 2):
a = a - n;
if (n == 1):
return ans;
return 0;
# To perform the Solovay-Strassen Primality Test
def solovoyStrassen(p, iterations):
if (p < 2):
return False;
if (p != 2 and p % 2 == 0):
return False;
for i in range(iterations):
# Generate a random number a
a = random.randrange(p - 1) + 1;
jacobian = (p + calculateJacobian(a, p)) % p;
mod = modulo(a, (p - 1) / 2, p);
if (jacobian == 0 or mod != jacobian):
return False;
return True;
# Driver Code
iterations = 100;
num1 = 150;
num2 = 131;
if (solovoyStrassen(num1, iterations)):
print(num1, "is prime ");
else:
print(num1, "is not prime");
if (solovoyStrassen(num2, iterations)):
print(num2, "is prime");
else:
print(num2, "is not prime");
#Reference: https://www.geeksforgeeks.org/primality-test-set-4-solovay-strassen/