VOOZH about

URL: https://www.geeksforgeeks.org/dsa/solovay-strassen-method-of-primality-test/

⇱ Solovay-Strassen method of Primality Test - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Solovay-Strassen method of Primality Test

Last Updated : 23 Jul, 2025

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:

  • Legendre Symbol (a/p)
  • Jacobian Symbol (a/n)

These symbols help us determine properties of numbers in a structured way.

Legendre Symbol (a/p)

This is a special way to compare two numbers:

  • a: Any integer
  • p: A prime number

It is written as (a/p) and tells us one of the following three things:

  1. (a/p) = 0 → If a is divisible byp, meaning a % p == 0
  2. (a/p) = 1 → if there exists an integer k such that k2 = a(mod p)
  3. (a/p) = -1 → Otherwise (if the above two cases are false)

Example

  • If p = 7, and a = 9, then:
    (9/7) = 1 because 9 has a square root 3 (since 3² ≡ 9).
  • If a = 2, then (2/7) = -1 because 2 is not a square modulo 7.

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.

Jacobian Symbol (a/n)

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.

The Solovay–Strassen Primality Test

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:

  1. a^((n-1)/2) mod n (using fast exponentiation)
  2. (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.

Why Do We Repeat This Test Multiple Times?

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.

  • If the test fails even once, n is definitely composite.
  • If the test passes many times, then n is probably prime.

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.

Comment