C++ Program to Implement Fermat's Little Theorem

In this article, you will learn about the implementation of the Fermat's Little Theorem in C++. But before going to its implementation, you must know about the Fermat's Little Theorem.

What is the Fermat's Little Theorem?

The Fermat's Little Theorem, named for the French mathematician Pierre de Fermat, who first proposed it in the 17th century, is fundamental in number theory. The theorem provides a remarkable relationship between modular arithmetic and prime numbers.

According to Fermat's Little Theorem, if p is a prime number, the number "a p - a" an is an integer multiple of p for every integer a.

ap-1 ≡ 1 (mod p)

Special Case

Fermat's Little Theorem may be used to prove that a p-1-1 is an integer multiple of p when an is not divisible by p.

ap-1 ≡ 1 (mod p)

OR

ap-1 % p = 1

In this case, p cannot divide a.

Some key points about Fermat's Little Theorem

There are several key points about the Fermat's Little Theorem. Some main key points of the Fermat's Little Theorem are as follows:

  • Prime Numbers: Fermat's Little Theorem focuses only on prime numbers. It provides a method for determining if a given number is prime. However, it is probabilistic and not always definitive.
  • Cryptography: The theory's applications in cryptography include the RSA encryption algorithm. Since it offers an effective method of determining the modular inverse, it serves as a basis for generating public and private keys.
  • Testing for Probabilistic Primality: Fermat's Little Theorem offers a probabilistic primality test, but it is insufficient to establish primality independently, particularly when dealing with big numbers. For more accurate primality testing, other tests, such as the Miller-Rabin test, can be used along with Fermat's Little Theorem.
  • Modular Arithmetic: Fermat's Little Theorem is a key concept in modular arithmetic. It provides an effective way to find modular inverses. "a^p-2" is the modular inverse of an integer a modulo p for any integer a that is not divisible by a prime number p. Cryptographic algorithms like RSA make considerable use of this characteristic for both encryption and decryption.

Example:

Let us take an example to illustrate the Fermat's Little Theorem in C++.

Output:

Enter the to find modular multiplicative inverse: 5
Enter the prime modulo value: 7
The multiplicative inverse of 5 modulo 7 is: 3

Explanation:

  • This C++ program applies Fermat's Little Theorem to determine a number's modular multiplicative inverse modulo a prime. The power_Modulo function uses binary exponentiation to compute (base^expo_nent) mod modulo efficiently.
  • The finding_Multiplicative_Inverse Function employs Fermat's Little Theorem to determine a number's multiplicative inverse modulo a prime.
  • The user provides the prime modulo value and the number to determine the multiplicative inverse for the main Function. After that, the finding_Multiplicative_Inverse Function is used by the program to compute the multiplicative inverse, and the result is printed.

Conclusion:

Fermat's Little Theorem is a powerful and versatile tool that can be used for many applications in mathematics and computer science, such as number theory, cryptography, and primality testing. Because of its Elegance and simplicity, it is a fundamental component of modern computer mathematics.






Latest Courses