|
30 | 30 | * a^{m-2} &≡& a^{-1} \;\text{mod}\; m
|
31 | 31 | * \f}
|
32 | 32 | *
|
33 |
| - * We will find the exponent using binary exponentiation. Such that the |
34 |
| - * algorithm works in \f$O(\log m)\f$ time. |
| 33 | + * We will find the exponent using binary exponentiation such that the |
| 34 | + * algorithm works in \f$O(\log n)\f$ time. |
35 | 35 | *
|
36 | 36 | * Examples: -
|
37 | 37 | * * a = 3 and m = 7
|
|
43 | 43 | * (as \f$a\times a^{-1} = 1\f$)
|
44 | 44 | */
|
45 | 45 |
|
46 |
| -#include <iostream> |
47 |
| -#include <vector> |
| 46 | +#include <cassert> /// for assert |
| 47 | +#include <cstdint> /// for std::int64_t |
| 48 | +#include <iostream> /// for IO implementations |
48 | 49 |
|
49 |
| -/** Recursive function to calculate exponent in \f$O(\log n)\f$ using binary |
50 |
| - * exponent. |
| 50 | +/** |
| 51 | + * @namespace math |
| 52 | + * @brief Maths algorithms. |
| 53 | + */ |
| 54 | +namespace math { |
| 55 | +/** |
| 56 | + * @namespace modular_inverse_fermat |
| 57 | + * @brief Calculate modular inverse using Fermat's Little Theorem. |
| 58 | + */ |
| 59 | +namespace modular_inverse_fermat { |
| 60 | +/** |
| 61 | + * @brief Calculate exponent with modulo using binary exponentiation in \f$O(\log b)\f$ time. |
| 62 | + * @param a The base |
| 63 | + * @param b The exponent |
| 64 | + * @param m The modulo |
| 65 | + * @return The result of \f$a^{b} % m\f$ |
51 | 66 | */
|
52 |
| -int64_t binExpo(int64_t a, int64_t b, int64_t m) { |
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; |
| 67 | +std::int64_t binExpo(std::int64_t a, std::int64_t b, std::int64_t m) { |
| 68 | + a %= m; |
| 69 | + std::int64_t res = 1; |
| 70 | + while (b > 0) { |
| 71 | + if (b % 2 != 0) { |
| 72 | + res = res * a % m; |
62 | 73 | }
|
63 |
| - return res; |
| 74 | + a = a * a % m; |
| 75 | + // Dividing b by 2 is similar to right shift by 1 bit |
| 76 | + b >>= 1; |
| 77 | + } |
| 78 | + return res; |
64 | 79 | }
|
65 |
| - |
66 |
| -/** Prime check in \f$O(\sqrt{m})\f$ time. |
| 80 | +/** |
| 81 | + * @brief Check if an integer is a prime number in \f$O(\sqrt{m})\f$ time. |
| 82 | + * @param m An intger to check for primality |
| 83 | + * @return true if the number is prime |
| 84 | + * @return false if the number is not prime |
67 | 85 | */
|
68 |
| -bool isPrime(int64_t m) { |
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 |
| - } |
| 86 | +bool isPrime(std::int64_t m) { |
| 87 | + if (m <= 1) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + for (std::int64_t i = 2; i * i <= m; i++) { |
| 91 | + if (m % i == 0) { |
| 92 | + return false; |
77 | 93 | }
|
78 |
| - return true; |
| 94 | + } |
| 95 | + return true; |
| 96 | +} |
| 97 | +/** |
| 98 | + * @brief calculates the modular inverse. |
| 99 | + * @param a Integer value for the base |
| 100 | + * @param m Integer value for modulo |
| 101 | + * @return The result that is the modular inverse of a modulo m |
| 102 | + */ |
| 103 | +std::int64_t modular_inverse(std::int64_t a, std::int64_t m) { |
| 104 | + while (a < 0) { |
| 105 | + a += m; |
| 106 | + } |
| 107 | + |
| 108 | + // Check for invalid cases |
| 109 | + if (!isPrime(m) || a == 0) { |
| 110 | + return -1; // Invalid input |
| 111 | + } |
| 112 | + |
| 113 | + return binExpo(a, m - 2, m); // Fermat's Little Theorem |
| 114 | +} |
| 115 | +} // namespace modular_inverse_fermat |
| 116 | +} // namespace math |
| 117 | + |
| 118 | +/** |
| 119 | + * @brief Self-test implementation |
| 120 | + * @return void |
| 121 | + */ |
| 122 | +static void test() { |
| 123 | + assert(math::modular_inverse_fermat::modular_inverse(0, 97) == -1); |
| 124 | + assert(math::modular_inverse_fermat::modular_inverse(15, -2) == -1); |
| 125 | + assert(math::modular_inverse_fermat::modular_inverse(3, 10) == -1); |
| 126 | + assert(math::modular_inverse_fermat::modular_inverse(3, 7) == 5); |
| 127 | + assert(math::modular_inverse_fermat::modular_inverse(1, 101) == 1); |
| 128 | + assert(math::modular_inverse_fermat::modular_inverse(-1337, 285179) == 165519); |
| 129 | + assert(math::modular_inverse_fermat::modular_inverse(123456789, 998244353) == 25170271); |
| 130 | + assert(math::modular_inverse_fermat::modular_inverse(-9876543210, 1000000007) == 784794281); |
79 | 131 | }
|
80 | 132 |
|
81 | 133 | /**
|
82 |
| - * Main function |
| 134 | + * @brief Main function |
| 135 | + * @return 0 on exit |
83 | 136 | */
|
84 | 137 | int main() {
|
85 |
| - int64_t a, m; |
86 |
| - // Take input of a and m. |
87 |
| - std::cout << "Computing ((a^(-1))%(m)) using Fermat's Little Theorem"; |
88 |
| - std::cout << std::endl << std::endl; |
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."; |
96 |
| - std::cout << std::endl; |
97 |
| - } |
| 138 | + test(); // run self-test implementation |
| 139 | + return 0; |
98 | 140 | }
|
0 commit comments