forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add modular_inverse_simple (TheAlgorithms#1937)
* fix: power_of_two algorithm redundant conditional 'else' * feat : add modular_inverse_simple * updating DIRECTORY.md * fix : according lint rules * fix : removed macro and initialize variable aux * fix: remove namespace std like default * feat : add unsigned type for block negative number and improving description * fix: defined in header <cstdint> for use uint64_t type * fix: improving descriptive comments * fix: remove redundant lib * fix: improvinf "brief" and "details" acording suggestion in review * fix: improving descrition of function imod acording suggestion in review * fix: improving descrition acording suggestion in review * Update modular_inverse_simple.cpp * Apply suggestions from code review * clang-format and clang-tidy fixes for 768a99c * Apply suggestions from code review * Apply suggestions from code review * updating DIRECTORY.md Co-authored-by: John Law <[email protected]> Co-authored-by: David Leal <[email protected]>
- Loading branch information
1 parent
2a076c1
commit 274cab6
Showing
2 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @file | ||
* @brief Simple implementation of [modular multiplicative | ||
* inverse](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) | ||
* | ||
* @details | ||
* this algorithm calculates the modular inverse x^{-1} \mod y iteratively | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for IO operations | ||
|
||
/** | ||
* @brief Function imod | ||
* Calculates the modular inverse of x with respect to y, x^{-1} \mod y | ||
* @param x number | ||
* @param y number | ||
* @returns the modular inverse | ||
*/ | ||
uint64_t imod(uint64_t x, uint64_t y) { | ||
uint64_t aux = 0; // auxiliary variable | ||
uint64_t itr = 0; // iteration counter | ||
|
||
do { // run the algorithm while not find the inverse | ||
aux = y * itr + 1; | ||
itr++; | ||
} while (aux % x); // while module aux % x non-zero | ||
|
||
return aux / x; | ||
} | ||
|
||
/** | ||
* @brief self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
std::cout << "First case testing... \n"; | ||
// for a = 3 and b = 11 return 4 | ||
assert(imod(3, 11) == 4); | ||
std::cout << "\nPassed!\n"; | ||
|
||
std::cout << "Second case testing... \n"; | ||
// for a = 3 and b = 26 return 9 | ||
assert(imod(3, 26) == 9); | ||
std::cout << "\nPassed!\n"; | ||
|
||
std::cout << "Third case testing... \n"; | ||
// for a = 7 and b = 26 return 15 | ||
assert(imod(7, 26) == 15); | ||
std::cout << "\nPassed!\n"; | ||
|
||
std::cout << "\nAll test cases have successfully passed!\n"; | ||
} | ||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
}; |