Given a number n, check if it is prime or not. We have introduced and discussed School and Fermat methods for primality testing.
In this post, Miller-Rabin method is discussed. This method is a probabilistic method (Like Fermat), but it generally preferred over Fermat’s method.
// It returns false if n is composite and returns true if n // is probably prime. k is an input parameter that determines // accuracy level. Higher value of k indicates more accuracy. bool isPrime(int n, int k) 1) Handle base cases for n < 3 2) If n is even, return false. 3) Find an odd number d such that n-1 can be written as d*2r. Note that since n is odd, (n-1) must be even and r must be greater than 0. 4) Do following k times if (millerTest(n, d) == false) return false 5) Return true. // This function is called for all k trials. It returns // false if n is composite and returns false if n is probably // prime. // d is an odd number such that d*2r = n-1 for some r >= 1 bool millerTest(int n, int d) 1) Pick a random number 'a' in range [2, n-2] 2) Compute: x = pow(a, d) % n 3) If x == 1 or x == n-1, return true. // Below loop mainly runs 'r-1' times. 4) Do following while d doesn't become n-1. a) x = (x*x) % n. b) If (x == 1) return false. c) If (x == n-1) return true.
Input: n = 13, k = 2. 1) Compute d and r such that d*2r = n-1, d = 3, r = 2. 2) Call millerTest k times. 1st Iteration: 1) Pick a random number 'a' in range [2, n-2] Suppose a = 4 2) Compute: x = pow(a, d) % n x = 43 % 13 = 12 3) Since x = (n-1), return true. IInd Iteration: 1) Pick a random number 'a' in range [2, n-2] Suppose a = 5 2) Compute: x = pow(a, d) % n x = 53 % 13 = 8 3) x neither 1 nor 12. 4) Do following (r-1) = 1 times a) x = (x * x) % 13 = (8 * 8) % 13 = 12 b) Since x = (n-1), return true. Since both iterations return true, we return true.
Below is the implementation of above algorithm.
All primes smaller than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
How does this work?
Below are some important facts behind the algorithm:
- Fermat’s theorem states that, If n is a prime number, then for every a, 1 <= a < n, an-1 % n = 1
- Base cases make sure that n must be odd. Since n is odd, n-1 must be even. And an even number can be written as d * 2s where d is an odd number and s > 0.
- From above two points, for every randomly picked number in range [2, n-2], value of ad*2r % n must be 1.
- As per Euclid’s Lemma, if x2 % n = 1 or (x2 – 1) % n = 0 or (x-1)(x+1)% n = 0. Then, for n to be prime, either n divides (x-1) or n divides (x+1). Which means either x % n = 1 or x % n = -1.
- From points 2 and 3, we can conclude
For n to be prime, either ad % n = 1 OR ad*2i % n = -1 for some i, where 0 <= i <= r-1.
Next Article :
Primality Test | Set 4 (Solovay-Strassen)
This article is contributed Ruchir Garg. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- AKS Primality Test
- Lucas Primality Test
- Implementation of Wilson Primality test
- Primality Test | Set 2 (Fermat Method)
- Vantieghems Theorem for Primality Test
- Primality Test | Set 4 (Solovay-Strassen)
- Primality Test | Set 5(Using Lucas-Lehmer Series)
- Primality test for the sum of digits at odd places of a number
- Primality Test | Set 1 (Introduction and School Method)
- MAQ Software Interview Experience | Set 12 (Written Test)
- Paytm Interview Experience | Set 7 (Written Test Hyderabad)
- New Algorithm to Generate Prime Numbers from 1 to Nth Number
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Find the position of box which occupies the given ball