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 true 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Primality Test | Set 1 (Introduction and School Method)
- Primality Test | Set 5(Using Lucas-Lehmer Series)
- Primality Test | Set 4 (Solovay-Strassen)
- Primality Test | Set 2 (Fermat Method)
- AKS Primality Test
- Vantieghems Theorem for Primality Test
- Implementation of Wilson Primality test
- Primality test for the sum of digits at odd places of a number
- Lucas Primality Test
- Paytm Interview Experience | Set 7 (Written Test Hyderabad)
- MAQ Software Interview Experience | Set 12 (Written Test)
- Prime Number of Set Bits in Binary Representation | Set 2
- Prime Number of Set Bits in Binary Representation | Set 1
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count total set bits in all numbers from 1 to n | Set 2
- Count digits in a factorial | Set 2
- Power Set
- Josephus problem | Set 1 (A O(n) Solution)
- XOR of all subarray XORs | Set 2
- Chinese Remainder Theorem | Set 1 (Introduction)