# Java Program to Implement Solovay-Strassen Primality Test

«
»
This is a Java Program to Implement Solovay Strassen Primality Test Algorithm. Solovay Strassen Primality Test is an algorithm which is used to determine if a given number is prime or not.

Here is the source code of the Java Program to Implement Solovay Strassen Primality Test Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `/**`
2. ` ** Java Program to Implement SolovayStrassen Primality Test Algorithm`
3. ` **/`
4. ` `
5. `import java.util.Scanner;`
6. `import java.util.Random;`
7. ` `
8. `/** Class SolovayStrassen **/`
9. `public class SolovayStrassen`
10. `{`
11. `    /** Function to calculate jacobi (a/b) **/`
12. `    public long Jacobi(long a, long b)`
13. `    {`
14. `        if (b <= 0 || b % 2 == 0)`
15. `            return 0;`
16. `        long j = 1L;`
17. `        if (a < 0)`
18. `        {`
19. `            a = -a;`
20. `            if (b % 4 == 3)`
21. `                j = -j;`
22. `        }`
23. `        while (a != 0)`
24. `        {`
25. `            while (a % 2 == 0)`
26. `            {`
27. `                a /= 2;`
28. `                if (b % 8 == 3 || b % 8 == 5)`
29. `                    j = -j;`
30. `            }`
31. ` `
32. `            long temp = a;`
33. `            a = b;`
34. `            b = temp;`
35. ` `
36. `            if (a % 4 == 3 && b % 4 == 3)`
37. `                j = -j;`
38. `            a %= b;`
39. `        }`
40. `        if (b == 1)`
41. `            return j;`
42. `        return 0;`
43. `    }`
44. `    /** Function to check if prime or not **/`
45. `    public boolean isPrime(long n, int iteration)`
46. `    {`
47. `        /** base case **/`
48. `        if (n == 0 || n == 1)`
49. `            return false;`
50. `        /** base case - 2 is prime **/`
51. `        if (n == 2)`
52. `            return true;`
53. `        /** an even number other than 2 is composite **/`
54. `        if (n % 2 == 0)`
55. `            return false;`
56. ` `
57. `        Random rand = new Random();`
58. `        for (int i = 0; i < iteration; i++)`
59. `        {`
60. `            long r = Math.abs(rand.nextLong());            `
61. `            long a = r % (n - 1) + 1;`
62. `            long jacobian = (n + Jacobi(a, n)) % n;`
63. `            long mod = modPow(a, (n - 1)/2, n);`
64. `            if(jacobian == 0 || mod != jacobian) `
65. `                return false;`
66. `        }`
67. `        return true;        `
68. `    }`
69. `    /** Function to calculate (a ^ b) % c **/`
70. `    public long modPow(long a, long b, long c)`
71. `    {`
72. `        long res = 1;`
73. `        for (int i = 0; i < b; i++)`
74. `        {`
75. `            res *= a;`
76. `            res %= c; `
77. `        }`
78. `        return res % c;`
79. `    }    `
80. `    /** Main function **/`
81. `    public static void main (String[] args) `
82. `    {`
83. `        Scanner scan = new Scanner(System.in);`
84. `        System.out.println("SolovayStrassen Primality Algorithm Test\n");`
85. `        /** Make an object of SolovayStrassen class **/`
86. `        SolovayStrassen ss = new SolovayStrassen();`
87. `        /** Accept number **/`
88. `        System.out.println("Enter number\n");`
89. `        long num = scan.nextLong();`
90. `        /** Accept number of iterations **/`
91. `        System.out.println("\nEnter number of iterations");`
92. `        int k = scan.nextInt();`
93. `        /** check if prime **/`
94. `        boolean prime = ss.isPrime(num, k);`
95. `        if (prime)`
96. `            System.out.println("\n"+ num +" is prime");`
97. `        else`
98. `            System.out.println("\n"+ num +" is composite");        `
99. `    }`
100. `}`

```SolovayStrassen Primality Algorithm Test

Enter number

9997777

Enter number of iterations
1

9997777 is prime```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 