Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
modular_inverse_fermat_little_theorem.cpp File Reference

C++ Program to find the modular inverse using Fermat's Little Theorem More...

#include <iostream>
#include <vector>
Include dependency graph for modular_inverse_fermat_little_theorem.cpp:

Functions

int64_t binExpo (int64_t a, int64_t b, int64_t m)
 
bool isPrime (int64_t m)
 
int main ()
 

Detailed Description

C++ Program to find the modular inverse using Fermat's Little Theorem

Fermat's Little Theorem state that

\[ϕ(m) = m-1\]

where \(m\) is a prime number.

\begin{eqnarray*} a \cdot x &≡& 1 \;\text{mod}\; m\\ x &≡& a^{-1} \;\text{mod}\; m \end{eqnarray*}

Using Euler's theorem we can modify the equation.

\[ a^{ϕ(m)} ≡ 1 \;\text{mod}\; m \]

(Where '^' denotes the exponent operator)

Here 'ϕ' is Euler's Totient Function. For modular inverse existence 'a' and 'm' must be relatively primes numbers. To apply Fermat's Little Theorem is necessary that 'm' must be a prime number. Generally in many competitive programming competitions 'm' is either 1000000007 (1e9+7) or 998244353.

We considered m as large prime (1e9+7). \(a^{ϕ(m)} ≡ 1 \;\text{mod}\; m\) (Using Euler's Theorem) \(ϕ(m) = m-1\) using Fermat's Little Theorem. \(a^{m-1} ≡ 1 \;\text{mod}\; m\) Now multiplying both side by \(a^{-1}\).

\begin{eqnarray*} a^{m-1} \cdot a^{-1} &≡& a^{-1} \;\text{mod}\; m\\ a^{m-2} &≡& a^{-1} \;\text{mod}\; m \end{eqnarray*}

We will find the exponent using binary exponentiation. Such that the algorithm works in \(O(\log m)\) time.

Examples: -

  • a = 3 and m = 7
  • \(a^{-1} \;\text{mod}\; m\) is equivalent to \(a^{m-2} \;\text{mod}\; m\)
  • \(3^5 \;\text{mod}\; 7 = 243 \;\text{mod}\; 7 = 5\)
    Hence, \(3^{-1} \;\text{mod}\; 7 = 5\) or \(3 \times 5 \;\text{mod}\; 7 = 1 \;\text{mod}\; 7\) (as \(a\times a^{-1} = 1\))

Function Documentation

◆ binExpo()

int64_t binExpo ( int64_t a,
int64_t b,
int64_t m )

Recursive function to calculate exponent in \(O(\log n)\) using binary exponent.

52 {
53 a %= m;
54 int64_t res = 1;
55 while (b > 0) {
56 if (b % 2) {
57 res = res * a % m;
58 }
59 a = a * a % m;
60 // Dividing b by 2 is similar to right shift.
61 b >>= 1;
62 }
63 return res;
64}

◆ isPrime()

bool isPrime ( int64_t m)

Prime check in \(O(\sqrt{m})\) time.

68 {
69 if (m <= 1) {
70 return false;
71 } else {
72 for (int64_t i = 2; i * i <= m; i++) {
73 if (m % i == 0) {
74 return false;
75 }
76 }
77 }
78 return true;
79}

◆ main()

int main ( void )

Main function

84 {
85 int64_t a, m;
86 // Take input of a and m.
87 std::cout << "Computing ((a^(-1))%(m)) using Fermat's Little Theorem";
89 std::cout << "Give input 'a' and 'm' space separated : ";
90 std::cin >> a >> m;
91 if (isPrime(m)) {
92 std::cout << "The modular inverse of a with mod m is (a^(m-2)) : ";
93 std::cout << binExpo(a, m - 2, m) << std::endl;
94 } else {
95 std::cout << "m must be a prime number.";
97 }
98}
T endl(T... args)
bool isPrime(int64_t m)
Definition modular_inverse_fermat_little_theorem.cpp:68
int64_t binExpo(int64_t a, int64_t b, int64_t m)
Definition modular_inverse_fermat_little_theorem.cpp:52
Here is the call graph for this function: