forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolovayStrassenTest.java
More file actions
78 lines (76 loc) · 1.75 KB
/
SolovayStrassenTest.java
File metadata and controls
78 lines (76 loc) · 1.75 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
import java.util.Scanner;
import java.util.Random;
class SolovayStrassenTest
{
public static long power(long a,long n, long p)
{
long res = 1;
a = a % p;
while (n > 0)
{
if ((n & 1) == 1)
res = (res * a) % p;
n = n >> 1;
a = (a * a) % p;
}
return res;
}
public static long calculateJacobian(long a, long b)
{
if (b <= 0 || b % 2 == 0)
return 0;
long j = 1L;
if (a < 0)
{
a = -a;
if (b % 4 == 3)
j = -j;
}
while (a != 0)
{
while (a % 2 == 0)
{
a /= 2;
if (b % 8 == 3 || b % 8 == 5)
j = -j;
}
long temp = a;
a = b;
b = temp;
if (a % 4 == 3 && b % 4 == 3)
j = -j;
a %= b;
}
if (b == 1)
return j;
return 0;
}
public static boolean isPrime(int n, int k)
{
if (n < 2)
return false;
if (n != 2 && n % 2 == 0)
return false;
Random rand = new Random();
for (int i = 0; i < k; i++)
{
long a = Math.abs(rand.nextLong()) % (n - 1) + 1;
long jacobian = (n + calculateJacobian(a, n)) % n;
long mod = power(a, (n - 1) / 2, n);
if (jacobian == 0 || mod != jacobian)
return false;
}
return true;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int num;
num = sc.nextInt();
if(isPrime(num, 50))
System.out.println("Number is Prime");
else
System.out.println("Number is not Prime");
}
}