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 < iostream > // / for IO implementations
48
48
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$
51
65
*/
52
66
int64_t binExpo (int64_t a, int64_t b, int64_t m) {
53
67
a %= m;
@@ -62,8 +76,11 @@ int64_t binExpo(int64_t a, int64_t b, int64_t m) {
62
76
}
63
77
return res;
64
78
}
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
67
84
*/
68
85
bool isPrime (int64_t m) {
69
86
if (m <= 1 ) {
@@ -77,22 +94,40 @@ bool isPrime(int64_t m) {
77
94
}
78
95
return true ;
79
96
}
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
+ }
80
125
81
126
/* *
82
- * Main function
127
+ * @brief Main function
128
+ * @return 0 on exit
83
129
*/
84
130
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 ;
98
133
}
0 commit comments