TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
C++ Program to find the modular inverse using Fermat's Little Theorem More...
#include <cassert>
#include <cstdint>
#include <iostream>
Go to the source code of this file.
Namespaces | |
namespace | math |
for assert | |
namespace | modular_inverse_fermat |
Calculate modular inverse using Fermat's Little Theorem. | |
Functions | |
std::int64_t | math::modular_inverse_fermat::binExpo (std::int64_t a, std::int64_t b, std::int64_t m) |
Calculate exponent with modulo using binary exponentiation in \(O(\log b)\) time. | |
bool | math::modular_inverse_fermat::isPrime (std::int64_t m) |
Check if an integer is a prime number in \(O(\sqrt{m})\) time. | |
std::int64_t | math::modular_inverse_fermat::modular_inverse (std::int64_t a, std::int64_t m) |
calculates the modular inverse. | |
static void | test () |
Self-test implementation. | |
int | main () |
Main function. | |
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 n)\) time.
Examples: -
Definition in file modular_inverse_fermat_little_theorem.cpp.
std::int64_t math::modular_inverse_fermat::binExpo | ( | std::int64_t | a, |
std::int64_t | b, | ||
std::int64_t | m ) |
Calculate exponent with modulo using binary exponentiation in \(O(\log b)\) time.
a | The base |
b | The exponent |
m | The modulo |
Definition at line 67 of file modular_inverse_fermat_little_theorem.cpp.
bool math::modular_inverse_fermat::isPrime | ( | std::int64_t | m | ) |
Check if an integer is a prime number in \(O(\sqrt{m})\) time.
m | An intger to check for primality |
Definition at line 86 of file modular_inverse_fermat_little_theorem.cpp.
int main | ( | void | ) |
Main function.
Definition at line 137 of file modular_inverse_fermat_little_theorem.cpp.
std::int64_t math::modular_inverse_fermat::modular_inverse | ( | std::int64_t | a, |
std::int64_t | m ) |
calculates the modular inverse.
a | Integer value for the base |
m | Integer value for modulo |
Definition at line 103 of file modular_inverse_fermat_little_theorem.cpp.
|
static |
Self-test implementation.
Definition at line 122 of file modular_inverse_fermat_little_theorem.cpp.