![]() |
VOOZH | about |
We have already been introduced to primality testing in the previous articles in this series.
The Solovay–Strassen test is a probabilistic algorithm used to check if a number is prime or composite (not prime). It is not 100% accurate but gives a high probability of correctness when run multiple times. Before jumping into the test, let's break down some key concepts:
To perform the test, we need to understand two important mathematical symbols:
These symbols help us determine properties of numbers in a structured way.
This is a special way to compare two numbers:
a: Any integerp: A prime numberIt is written as (a/p) and tells us one of the following three things:
- (a/p) = 0 → If
ais divisible byp, meaninga % p == 0- (a/p) = 1 → if there exists an integer k such that k2 = a(mod p)
- (a/p) = -1 → Otherwise (if the above two cases are false)
Example
- If
p = 7, anda = 9, then:
(9/7) = 1 because9has a square root3(since3² ≡ 9).- If
a = 2, then (2/7) = -1 because2is not a square modulo7.Mathematician Euler discovered a useful shortcut to compute (a/p):
(a/p) = a(p-1)/2 mod p
This formula helps in quickly checking the Legendre symbol.
The Jacobian symbol is a generalization of the Legendre symbol, but it works when n is not necessarily a prime. It is calculated by breaking down n into prime factors and applying the Legendre symbol to each factor.
If n is a prime number, then:
(a/n) = (a/p)
So, Jacobian symbol = Legendre symbol when n is prime.
Now that we understand these symbols, let's go step by step through the primality test.
Step 1: Choose a Random Number
- Pick a random number a, such that 2 ≤ a ≤ n - 1.
- This number a acts as a "witness" to help determine whether n is prime.
Step 2: Check the GCD (Greatest Common Divisor)
- Compute gcd(a, n) (the largest number that divides both a and n).
- If gcd(a, n) > 1, then n is definitely composite (not prime).
- This step ensures n has no common factors with a.
Step 3: Compute Two Values
We compute two important values:
- a^((n-1)/2) mod n (using fast exponentiation)
- (a/n) (using the Jacobian symbol)
Step 4: Compare the Two Values
- If these two values do not match, then n is composite.
- If they are equal, then n is probably prime.
Since this is a probabilistic test, we cannot be 100% sure that n is prime after one test. So, we repeat the test with different random values of a multiple times.
Output :
15 is composite
13 is prime
Time Complexity: Using fast algorithms for modular exponentiation, the running time of this algorithm is O(k*(log n)3), where k is the number of different values we test.
Auxiliary Space: O(1) as it is using constant space for variables
Accuracy: It is possible for the algorithm to return an incorrect answer. If the input n is indeed prime, then the output will always probably be correctly prime. However, if the input n is composite, then it is possible for the output to probably be incorrect prime. The number n is then called an Euler-Jacobi pseudoprime.