TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
modular_inverse_simple.cpp
Go to the documentation of this file.
1
10
#include <cassert>
11
#include <cstdint>
12
#include <iostream>
13
21
uint64_t
imod
(uint64_t x, uint64_t y) {
22
uint64_t aux = 0;
// auxiliary variable
23
uint64_t itr = 0;
// iteration counter
24
25
do
{
// run the algorithm while not find the inverse
26
aux = y * itr + 1;
27
itr++;
28
}
while
(aux % x);
// while module aux % x non-zero
29
30
return
aux / x;
31
}
32
37
static
void
test
() {
38
std::cout <<
"First case testing... \n"
;
39
// for a = 3 and b = 11 return 4
40
assert(
imod
(3, 11) == 4);
41
std::cout <<
"\nPassed!\n"
;
42
43
std::cout <<
"Second case testing... \n"
;
44
// for a = 3 and b = 26 return 9
45
assert(
imod
(3, 26) == 9);
46
std::cout <<
"\nPassed!\n"
;
47
48
std::cout <<
"Third case testing... \n"
;
49
// for a = 7 and b = 26 return 15
50
assert(
imod
(7, 26) == 15);
51
std::cout <<
"\nPassed!\n"
;
52
53
std::cout <<
"\nAll test cases have successfully passed!\n"
;
54
}
59
int
main
() {
60
test
();
// run self-test implementations
61
};
imod
uint64_t imod(uint64_t x, uint64_t y)
for assert
Definition
modular_inverse_simple.cpp:21
test
static void test()
self-test implementations
Definition
modular_inverse_simple.cpp:37
main
int main()
Main function.
Definition
modular_inverse_simple.cpp:59
math
modular_inverse_simple.cpp
Generated by
1.12.0