Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : add modular_inverse_simple #1937

Merged
merged 23 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c4319f7
fix: power_of_two algorithm redundant conditional 'else'
CarlosZoft Feb 10, 2022
bb0e472
Merge branch 'TheAlgorithms:master' into master
CarlosZoft Feb 15, 2022
efaabc3
feat : add modular_inverse_simple
CarlosZoft Feb 15, 2022
0a21f68
updating DIRECTORY.md
Panquesito7 Feb 15, 2022
98fe5a7
fix : according lint rules
CarlosZoft Feb 15, 2022
a7819b4
Merge branch 'master' of github.com:CarlosZoft/C-Plus-Plus
CarlosZoft Feb 15, 2022
6dbd3be
fix : removed macro and initialize variable aux
CarlosZoft Feb 15, 2022
e978641
fix: remove namespace std like default
CarlosZoft Feb 15, 2022
35d4b9f
feat : add unsigned type for block negative number and improving desc…
CarlosZoft Feb 15, 2022
f3ca0e5
Merge branch 'master' into master
CarlosZoft Mar 16, 2022
fec73a6
fix: defined in header <cstdint> for use uint64_t type
CarlosZoft Apr 24, 2022
9d7a29b
fix: improving descriptive comments
CarlosZoft Apr 24, 2022
beb32f8
fix: remove redundant lib
CarlosZoft Apr 24, 2022
b4899db
fix: improvinf "brief" and "details" acording suggestion in review
CarlosZoft Apr 24, 2022
2c2dc61
fix: improving descrition of function imod acording suggestion in review
CarlosZoft Apr 24, 2022
fa16e10
fix: improving descrition acording suggestion in review
CarlosZoft Apr 24, 2022
f49a954
Update modular_inverse_simple.cpp
poyea Apr 24, 2022
768a99c
Apply suggestions from code review
Panquesito7 Jun 9, 2022
8f8c504
clang-format and clang-tidy fixes for 768a99c8
Panquesito7 Jun 9, 2022
c806247
Apply suggestions from code review
Panquesito7 Jun 9, 2022
5d475c3
Apply suggestions from code review
Panquesito7 Jun 9, 2022
99b1ff7
Merge branch 'master' into master
Panquesito7 Jun 9, 2022
e90cf79
updating DIRECTORY.md
Panquesito7 Jun 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
* [Modular Division](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_division.cpp)
* [Modular Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_exponentiation.cpp)
* [Modular Inverse Fermat Little Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_inverse_fermat_little_theorem.cpp)
* [Modular Inverse Simple](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_inverse_simple.cpp)
* [N Bonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/n_bonacci.cpp)
* [N Choose R](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/n_choose_r.cpp)
* [Ncr Modulo P](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/ncr_modulo_p.cpp)
Expand Down
63 changes: 63 additions & 0 deletions math/modular_inverse_simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @file
* @brief simple implementation of adaption modular multiplicative inverse
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved
* algorithm
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved
*
* @details
* this algorithm calculate the modular inverse iteratively
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved
*
* @ref
* https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
*/

#include <cassert> /// for assert
#include <iostream> /// for IO operations

/**
* @brief Function imod
* Responsible for calculating the modular inverse from x % y
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved
* @param x number
* @param y number
* @returns the modular inverse
*/
uint64_t imod(uint64_t x, uint64_t y) { // positive integer expected
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved
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 is different of zero
CarlosZoft marked this conversation as resolved.
Show resolved Hide resolved

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
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
};