Skip to content

Commit fec9656

Browse files
authored
Update modular_inverse_fermat_little_theorem.cpp
1 parent f9fb58f commit fec9656

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

math/modular_inverse_fermat_little_theorem.cpp

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,25 @@
4343
* (as \f$a\times a^{-1} = 1\f$)
4444
*/
4545

46-
#include <iostream>
47-
#include <vector>
46+
#include <cassert> /// for assert
47+
#include <iostream> /// for IO implementations
4848

49-
/** Recursive function to calculate exponent in \f$O(\log n)\f$ using binary
50-
* exponent.
49+
/**
50+
* @namespace math
51+
* @brief Maths algorithms
52+
*/
53+
namespace math {
54+
/**
55+
* @namespace modular_inverse_fermat_little_theorem
56+
* @brief Calculate modular inverse using Fermat's Little Theorem
57+
*/
58+
namespace modular_inverse_fermat_little_theorem {
59+
/**
60+
* @brief Calculate exponent with modulo using divide-and-conquer.
61+
* @param a The base
62+
* @param b The exponent
63+
* @param m The modulo
64+
* @return res The result of \f$a^{b} % m\f$
5165
*/
5266
int64_t binExpo(int64_t a, int64_t b, int64_t m) {
5367
a %= m;
@@ -62,8 +76,11 @@ int64_t binExpo(int64_t a, int64_t b, int64_t m) {
6276
}
6377
return res;
6478
}
65-
66-
/** Prime check in \f$O(\sqrt{m})\f$ time.
79+
/**
80+
* @brief Check if a given integer is a prime number
81+
* @param m An intger to check for primality
82+
* @return 'true' if the number is prime
83+
* @return 'false' if the number is not prime
6784
*/
6885
bool isPrime(int64_t m) {
6986
if (m <= 1) {
@@ -77,22 +94,40 @@ bool isPrime(int64_t m) {
7794
}
7895
return true;
7996
}
97+
/**
98+
* @brief Main function to calculate modular inverse
99+
* @param a Integer value for base
100+
* @param m Integer value for modulo
101+
* @return the result that is the modular inverse of a modulo m
102+
*/
103+
int64_t modular_inverse(int64_t a, int64_t m) {
104+
// modulo m is not prime
105+
if (!isPrime(m)) {
106+
return -1; // Using -1 to mark for invalid input
107+
}
108+
return binExpo(a, m - 2, m);
109+
}
110+
} // namespace modular_inverse_fermat_little_theorem
111+
} // namespace math
112+
113+
/**
114+
* @brief Self-test implementation
115+
* @return void
116+
*/
117+
static void test() {
118+
assert(math::modular_inverse_fermat_little_theorem::modular_inverse(3, 6) == -1);
119+
assert(math::modular_inverse_fermat_little_theorem::modular_inverse(3, 7) == 5);
120+
assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1);
121+
assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1);
122+
assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1);
123+
assert(math::modular_inverse_fermat_little_theorem::modular_inverse(1, 1) == 1);
124+
}
80125

81126
/**
82-
* Main function
127+
* @brief Main function
128+
* @return 0 on exit
83129
*/
84130
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-
}
131+
test();
132+
return 0;
98133
}

0 commit comments

Comments
 (0)