diff --git a/DIRECTORY.md b/DIRECTORY.md index e9d51a2df..eaca7bc0d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -158,9 +158,6 @@ * [Quadratic Probing Hash Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/hashing/quadratic_probing_hash_table.cpp) * [Sha1](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/hashing/sha1.cpp) -## Linear Algebra - * [Gram Schmidt](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/linear_algebra/gram_schmidt.cpp) - ## Machine Learning * [A Star Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/a_star_search.cpp) * [Adaline Learning](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/adaline_learning.cpp) @@ -208,6 +205,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) @@ -237,6 +235,7 @@ * [Fast Fourier Transform](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/fast_fourier_transform.cpp) * [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gaussian_elimination.cpp) * [Golden Search Extrema](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/golden_search_extrema.cpp) + * [Gram Schmidt](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gram_schmidt.cpp) * [Inverse Fast Fourier Transform](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/inverse_fast_fourier_transform.cpp) * [Lu Decompose](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decompose.cpp) * [Lu Decomposition](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decomposition.h) diff --git a/math/modular_inverse_simple.cpp b/math/modular_inverse_simple.cpp new file mode 100644 index 000000000..813f0e0b4 --- /dev/null +++ b/math/modular_inverse_simple.cpp @@ -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 /// for assert +#include /// 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 +};