Implement Rabin-Miller Primality Test in C++



Rabin-Miller algorithm is a probabilistic primality test algorithm that is used to checks if a given number is likely to be a prime number or not. It is similar to the Fermat's primality test and the Solovay-Stressen test.

In this article, we have a number 'p'. Our task is to implement the Rabin-Miller algorithm to check if the given number is a prime number or not in C++.

Example

Here is an example of checking prime numbers using the Rabin-Miller algorithm:

Input:
p = 41

Output:
41 is a prime nnumber

Here is the explanation of the above example using the Rabin-Miller primality test:

Express 'p' as p-1 = d*2^r:

p-1 = 40 = 5*2^3
d = 5, r = 3

Calculating x = a^d mod p:

Let's assume a random 'a' = 6
x = 6^5 mod 41 = 6^5 = 7776 mod 41 = 27
x = 27
=> x != 1 or x != 40(p-1)

Squaring x to (r-1) times:

(r - 1) = 2, Squaring first time:
x = 27^2 mod 41 = 32 
=> x != 1 or x != 40

Squaring second time:
x = 32^2 mod 41 = 40
=> x = 40 = p - 1
=> x = 41 is a prime number 

Steps to Implement Rabin-Miller Primality Test

The following steps are used to implement the Rabin-Miller algorithm to check whether the given number is prime or not:

  • First, we represent 'p' in the form p - 1 = d * 2 ^ r. This gives us the value of 'd' and 'r'. This 'd' is later used as power of 'a' to find the value of 'x', and 'r' is the number of times we calculate the value of x.
  • Then we calculate the x = a^d mod p. Here, 'a' is a randomly generated number using the rand() function, and the iteration decides how many times we generate this random 'a' for calculating the value of x.
  • The value of a should be between [2, n-2]. For calculating the value of x, we have used the modulo() function.
  • Then we check if the value of x is equal to 1 or (p-1) using while (temp != p - 1 && mod != 1 && mod != p - 1).
  • If x is not equal to 1 or (p-1) then we square x to (r-1) times and find mod p. We have used the modMul() function for squaring and taking the mod of the result.
  • If x is equal to 1 or (p-1), then the given p is a prime number.

Implementing Rabin-Miller Primality Test in C++

The following code implements the above steps to implement the Rabin-Miller primality test in C++.

#include <iostream>
#include <stdlib.h>
#include <ctime>
#define ll long long
using namespace std;

// To prevent overflow
ll modMul(ll a, ll b, ll m) {  
    ll x = 0, y = a % m;
    while (b > 0) {
        if (b % 2 == 1) {
            x = (x + y) % m;
        }
        y = (y * 2) % m;
        b /= 2;
    }
    return x % m;
}

// For computing modular exponentiation
ll modulo(ll base, ll e, ll m) {
    ll x = 1;
    ll y = base % m;
    while (e > 0) {
        if (e % 2 == 1) {
            x = modMul(x, y, m);  
        }
        y = modMul(y, y, m);
        e /= 2;
    }
    return x % m;
}

// Miller-Rabin primality test
bool Miller(ll p, int iteration) {
    if (p < 2) return false;
    if (p == 2) return true;
    if (p % 2 == 0) return false;

    ll d = p - 1;
    while (d % 2 == 0) {
        d /= 2;
    }

    for (int i = 0; i < iteration; i++) {
        ll a = rand() % (p - 2) + 1;
        ll temp = d;
        ll mod = modulo(a, temp, p);
        while (temp != p - 1 && mod != 1 && mod != p - 1) {
            mod = modMul(mod, mod, p);
            temp *= 2;
        }
        if (mod != p - 1 && temp % 2 == 0) {  
            return false;
        }
    }
    return true;
}

int main() {
    srand(time(0)); 
    int iteration = 5;
    ll num = 47; 

    if (Miller(num, iteration)) 
        cout << num << " is a prime number" << endl;
    else
        cout << num << " is not a prime number" << endl;
    return 0;
}

The output of the above code is:

47 is a prime number
Updated on: 2025-05-06T19:01:02+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements