C++ Program to Implement Fermat’s Little Theorem



Fermat's Little Theorem states that if p is a prime number and a is an integer not divisible by p, then a^(p-1) is congruent to 1 modulo p. It can be represented as a(p-1) ≡ 1 (mod p). It can also be said that if any integer a is raised to the (p-1) where p is a prime and gcd(a, p) =1, then a^(p-1)-1 is divisible by p.

In this article, we have two integers i.e. 'a' and a prime number 'p' such that a is not divisible by p. Our task is to implement Fermat's Little theorem using these two integers in C++. Here is an example to demonstrate Fermat's Little theorem:

Input:
let a = 3 and p = 7;

Output:
Fermat's Little Theorem holds true for a = 3 and p = 7.

Here is the explanation of the above example:

a = 3, p = 7, gcd(3, 7) = 1
Fermat's Little Theorem: 
a(p-1) ≡ 1 (mod p)

36 = 729
729 mod 7 = 1
or,
36 ≡ 1 (mod 7)

Steps to Implement Fermat's Little Theorem

The steps to implement Fermat's Little theorem are as follows:

  • We have defined three functions. The first function isPrime() checks whether p is prime or not.
  • The modExp() function performs the calculation of the a(p-1) mod p. The while loop checks that the exponent should be non-zero.
  • If the base is odd, we update the result by multiplying it with the base. If the exponent is even, we square the base. Then we right shift the exponent and this loop continues until the exponent becomes zero.
  • The fermatsLittle() function checks two things. It checks if p is prime or not and checks if a and p are divisible or not. When both the conditions are satisfied it uses the modExp() function to return a(p-1) mod p = 1.

C++ Program to Implement Fermat's Little Theorem

The following code implements Fermat's Little theorem in C++ using the steps mentioned above:

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(long long n) {
    if (n <= 1) {
        return false;
    }
    if (n <= 3) {
        return true;
    }
    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }

    for (long long i = 5; i * i <= n; i += 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}

long long modExp(long long base, long long exponent, long long mod) {
    long long result = 1;
    base %= mod;

    while (exponent > 0) {
        if (exponent & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exponent >>= 1;
    }
    return result;
}

bool fermatsLittle(long long a, long long p) {
    if (!isPrime(p)) {
        return false;
    }
    if (a % p == 0) {
        return false;
    }
    return modExp(a, p - 1, p) == 1;
}

int main() {
    long long a = 3;
    long long p = 7; 

    if (fermatsLittle(a, p))
        cout << "Fermat's Little Theorem holds for a = " 
             << a << " and p = " << p << endl;
    else
        cout << "Fermat's Little Theorem does NOT hold for a = " 
             << a << " and p = " << p << endl;
    return 0;
}

The output of the above code is:

Fermat's Little Theorem holds for a = 3 and p = 7

Modular Inverse Using Fermat's Little Theorem

The modular inverse (a-1) can be calculated using Fermat's little theorem. It is represented as: a-1 = a (p-2) mod p.

Example

The following example calculates the modular inverse using Fermat's little theorem:

#include <iostream>
#include <cmath>
using namespace std;

// Function to check if a number is prime
bool isPrime(long long n) {
    if (n <= 1) {
        return false;
    }
    if (n <= 3) {
        return true;
    }
    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }

    for (long long i = 5; i * i <= n; i += 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

// Function to calculate modular exponentiation
long long modExp(long long base, long long exponent, long long mod) {
    long long result = 1;
    base %= mod;

    while (exponent > 0) {
        if (exponent % 2 == 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exponent >>= 1;
    }
    return result;
}

// Function to find modular inverse
long long modInverse(long long a, long long mod) {
    if (!isPrime(mod)) {
        cout << mod << " is not a prime number!" << endl;
        return -1;
    }
    if (a % mod == 0) {
        cout << "No modular inverse exists for a = " 
             << a << " mod " << mod << endl;
        return -1;
    }
    return modExp(a, mod - 2, mod);
}

int main() {
    long long a = 22;
    long long primeMod = 11;

    long long inverse = modInverse(a, primeMod);
    if (inverse != -1)
        cout << "Modular inverse of " << a << " under modulo " 
             << primeMod << " is: " << inverse << endl;

    return 0;
}

The output of the above code is:

No modular inverse exists for a = 22 mod 11
Updated on: 2025-04-23T17:07:46+05:30

873 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements