From 0450f6a1794e67b51ce3a109a457d2acbd33c038 Mon Sep 17 00:00:00 2001 From: shubhamamsa Date: Sun, 17 Jan 2021 23:09:01 +0530 Subject: [PATCH 1/6] feat: added modular division algorithm --- math/modular_division.cpp | 109 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 math/modular_division.cpp diff --git a/math/modular_division.cpp b/math/modular_division.cpp new file mode 100644 index 000000000..c243c8424 --- /dev/null +++ b/math/modular_division.cpp @@ -0,0 +1,109 @@ +/** + * @file + * @brief Division of two numbers under modulo + * + * @details To calculate division of two numbers under modulo p + * Modulo operator is not distributive under division, therefore + * we first have to calculate the inverse of divisor using + * [Fermat's little theorem](https://en.wikipedia.org/wiki/Fermat%27s_little_theorem) + * Now, we can multiply the dividend with the inverse of divisor + * and modulo is distributive over multiplication operation. + * Let, + * We have 3 numbers a, b, p + * To compute (a/b)%p + * (a/b)%p ≡ (a*(inverse(b)))%p ≡ ((a%p)*inverse(b)%p)%p + * NOTE: For the existence of inverse of 'b', 'b' and 'p' must be coprime + * For simplicity we take p as prime + * Time Complexity: O(log(b)) + * + * Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 --- (i) + Now the inverse of 3 is 2 + (24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 --- (ii) + (i) and (ii) are equal hence the answer is correct. + + * + * @see modular_inverse_fermat_little_theorem.cpp, modular_exponentiation.cpp + * + * @author [Shubham Yadav](https://github.com/shubhamamsa) + */ + +#include +#include + +/** + * @namespace math + */ +namespace math { + /** + * @brief This function calculates a raised to exponent b under modulo c using + * modular exponentiation. + * @param a integer base + * @param b unsigned integer exponent + * @param c integer modulo + * @return a raised to power b modulo c + */ + int power(int a, int b, int c) { + int ans = 1; /// Initialize the answer to be returned + a = a % c; /// Update a if it is more than or equal to c + if (a == 0) { + return 0; /// In case a is divisible by c; + } + while (b > 0) { + /// If b is odd, multiply a with answer + if (b & 1) { + ans = ((ans % c) * (a % c)) % c; + } + /// b must be even now + b = b >> 1; /// b = b/2 + a = ((a % c) * (a % c)) % c; + } + return ans; + } + + /** + * @brief This function calculates modular division + * @param a integer dividend + * @param b integer divisor + * @param p integer modulo + * @return a/b modulo c + */ + int mod_division(int a, int b, int p) { + int inverse = power(b, p-2, p)%p; /// Calculate the inverse of b + int result = ((a%p)*(inverse%p))%p; /// Calculate the final result + return result; + } +} + +/** + * Function for testing power function. + * test cases and assert statement. + * @returns `void` + */ +static void test() { + int test_case_1 = math::mod_division(8, 2, 2); + assert(test_case_1 == 0); + std::cout << "Test 1 Passed!" << std::endl; + int test_case_2 = math::mod_division(15, 3, 7); + assert(test_case_2 == 5); + std::cout << "Test 2 Passed!" << std::endl; + int test_case_3 = math::mod_division(10, 5, 2); + assert(test_case_3 == 0); + std::cout << "Test 3 Passed!" << std::endl; + int test_case_4 = math::mod_division(81, 3, 5); + assert(test_case_4 == 2); + std::cout << "Test 4 Passed!" << std::endl; + int test_case_5 = math::mod_division(12848, 73, 29); + assert(test_case_5 == 2); + std::cout << "Test 5 Passed!" << std::endl; +} + +/** + * @brief Main function + * @param argc commandline argument count (ignored) + * @param argv commandline array of arguments (ignored) + * @returns 0 on exit + */ +int main(int argc, char *argv[]) { + test(); // execute the tests + return 0; +} From 2ad5420a7c8b134bd2adc0c7ad624d9fa3c313de Mon Sep 17 00:00:00 2001 From: shubhamamsa Date: Mon, 18 Jan 2021 02:13:32 +0530 Subject: [PATCH 2/6] feat: Addressed comments for adding modular division algorithm --- math/modular_division.cpp | 101 ++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/math/modular_division.cpp b/math/modular_division.cpp index c243c8424..205b6c49a 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -1,7 +1,7 @@ /** * @file - * @brief Division of two numbers under modulo - * + * @brief An algorithm to divide two numbers under modulo p [Modular + * Division](https://www.geeksforgeeks.org/modular-division) * @details To calculate division of two numbers under modulo p * Modulo operator is not distributive under division, therefore * we first have to calculate the inverse of divisor using @@ -15,64 +15,67 @@ * NOTE: For the existence of inverse of 'b', 'b' and 'p' must be coprime * For simplicity we take p as prime * Time Complexity: O(log(b)) - * * Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 --- (i) Now the inverse of 3 is 2 (24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 --- (ii) (i) and (ii) are equal hence the answer is correct. - - * * @see modular_inverse_fermat_little_theorem.cpp, modular_exponentiation.cpp - * * @author [Shubham Yadav](https://github.com/shubhamamsa) */ -#include -#include +#include /// for assert +#include /// for IO operations /** * @namespace math + * @brief Mathematical algorithms */ namespace math { /** - * @brief This function calculates a raised to exponent b under modulo c using - * modular exponentiation. - * @param a integer base - * @param b unsigned integer exponent - * @param c integer modulo - * @return a raised to power b modulo c + * @namespace modular_division + * @brief Functions for Modular Division implementation */ - int power(int a, int b, int c) { - int ans = 1; /// Initialize the answer to be returned - a = a % c; /// Update a if it is more than or equal to c - if (a == 0) { - return 0; /// In case a is divisible by c; - } - while (b > 0) { - /// If b is odd, multiply a with answer - if (b & 1) { - ans = ((ans % c) * (a % c)) % c; - } - /// b must be even now - b = b >> 1; /// b = b/2 - a = ((a % c) * (a % c)) % c; - } - return ans; - } + namespace modular_division { + /** + * @brief This function calculates a raised to exponent b under modulo c using + * modular exponentiation. + * @param a integer base + * @param b unsigned integer exponent + * @param c integer modulo + * @return a raised to power b modulo c + */ + uint64_t power(uint64_t a, uint64_t b, uint64_t c) { + uint64_t ans = 1; /// Initialize the answer to be returned + a = a % c; /// Update a if it is more than or equal to c + if (a == 0) { + return 0; /// In case a is divisible by c; + } + while (b > 0) { + /// If b is odd, multiply a with answer + if (b & 1) { + ans = ((ans % c) * (a % c)) % c; + } + /// b must be even now + b = b >> 1; /// b = b/2 + a = ((a % c) * (a % c)) % c; + } + return ans; + } - /** - * @brief This function calculates modular division - * @param a integer dividend - * @param b integer divisor - * @param p integer modulo - * @return a/b modulo c - */ - int mod_division(int a, int b, int p) { - int inverse = power(b, p-2, p)%p; /// Calculate the inverse of b - int result = ((a%p)*(inverse%p))%p; /// Calculate the final result - return result; - } -} + /** + * @brief This function calculates modular division + * @param a integer dividend + * @param b integer divisor + * @param p integer modulo + * @return a/b modulo c + */ + uint64_t mod_division(uint64_t a, uint64_t b, uint64_t p) { + uint64_t inverse = power(b, p-2, p)%p; /// Calculate the inverse of b + uint64_t result = ((a%p)*(inverse%p))%p; /// Calculate the final result + return result; + } + } // namespace modular_division +} // namespace math /** * Function for testing power function. @@ -80,19 +83,19 @@ namespace math { * @returns `void` */ static void test() { - int test_case_1 = math::mod_division(8, 2, 2); + uint64_t test_case_1 = math::modular_division::mod_division(8, 2, 2); assert(test_case_1 == 0); std::cout << "Test 1 Passed!" << std::endl; - int test_case_2 = math::mod_division(15, 3, 7); + uint64_t test_case_2 = math::modular_division::mod_division(15, 3, 7); assert(test_case_2 == 5); std::cout << "Test 2 Passed!" << std::endl; - int test_case_3 = math::mod_division(10, 5, 2); + uint64_t test_case_3 = math::modular_division::mod_division(10, 5, 2); assert(test_case_3 == 0); std::cout << "Test 3 Passed!" << std::endl; - int test_case_4 = math::mod_division(81, 3, 5); + uint64_t test_case_4 = math::modular_division::mod_division(81, 3, 5); assert(test_case_4 == 2); std::cout << "Test 4 Passed!" << std::endl; - int test_case_5 = math::mod_division(12848, 73, 29); + uint64_t test_case_5 = math::modular_division::mod_division(12848, 73, 29); assert(test_case_5 == 2); std::cout << "Test 5 Passed!" << std::endl; } From 0e1e441ab35ecb72fec6623e19c93eb40d3debc7 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 17 Jan 2021 20:44:52 +0000 Subject: [PATCH 3/6] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ebe776444..09e589c21 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -154,6 +154,7 @@ * [Least Common Multiple](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/least_common_multiple.cpp) * [Magic Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/magic_number.cpp) * [Miller Rabin](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/miller_rabin.cpp) + * [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) * [N Choose R](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/n_choose_r.cpp) From b9cdab93547c234428bd599148588dbb794118df Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 17 Jan 2021 20:44:59 +0000 Subject: [PATCH 4/6] clang-format and clang-tidy fixes for 2ad5420a --- math/modular_division.cpp | 94 ++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/math/modular_division.cpp b/math/modular_division.cpp index 205b6c49a..67001b8cf 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -1,11 +1,12 @@ /** * @file - * @brief An algorithm to divide two numbers under modulo p [Modular + * @brief An algorithm to divide two numbers under modulo p [Modular * Division](https://www.geeksforgeeks.org/modular-division) * @details To calculate division of two numbers under modulo p * Modulo operator is not distributive under division, therefore * we first have to calculate the inverse of divisor using - * [Fermat's little theorem](https://en.wikipedia.org/wiki/Fermat%27s_little_theorem) + * [Fermat's little + theorem](https://en.wikipedia.org/wiki/Fermat%27s_little_theorem) * Now, we can multiply the dividend with the inverse of divisor * and modulo is distributive over multiplication operation. * Let, @@ -31,51 +32,52 @@ * @brief Mathematical algorithms */ namespace math { - /** - * @namespace modular_division - * @brief Functions for Modular Division implementation - */ - namespace modular_division { - /** - * @brief This function calculates a raised to exponent b under modulo c using - * modular exponentiation. - * @param a integer base - * @param b unsigned integer exponent - * @param c integer modulo - * @return a raised to power b modulo c - */ - uint64_t power(uint64_t a, uint64_t b, uint64_t c) { - uint64_t ans = 1; /// Initialize the answer to be returned - a = a % c; /// Update a if it is more than or equal to c - if (a == 0) { - return 0; /// In case a is divisible by c; - } - while (b > 0) { - /// If b is odd, multiply a with answer - if (b & 1) { - ans = ((ans % c) * (a % c)) % c; - } - /// b must be even now - b = b >> 1; /// b = b/2 - a = ((a % c) * (a % c)) % c; - } - return ans; +/** + * @namespace modular_division + * @brief Functions for Modular Division implementation + */ +namespace modular_division { +/** + * @brief This function calculates a raised to exponent b under modulo c using + * modular exponentiation. + * @param a integer base + * @param b unsigned integer exponent + * @param c integer modulo + * @return a raised to power b modulo c + */ +uint64_t power(uint64_t a, uint64_t b, uint64_t c) { + uint64_t ans = 1; /// Initialize the answer to be returned + a = a % c; /// Update a if it is more than or equal to c + if (a == 0) { + return 0; /// In case a is divisible by c; } + while (b > 0) { + /// If b is odd, multiply a with answer + if (b & 1) { + ans = ((ans % c) * (a % c)) % c; + } + /// b must be even now + b = b >> 1; /// b = b/2 + a = ((a % c) * (a % c)) % c; + } + return ans; +} - /** - * @brief This function calculates modular division - * @param a integer dividend - * @param b integer divisor - * @param p integer modulo - * @return a/b modulo c - */ - uint64_t mod_division(uint64_t a, uint64_t b, uint64_t p) { - uint64_t inverse = power(b, p-2, p)%p; /// Calculate the inverse of b - uint64_t result = ((a%p)*(inverse%p))%p; /// Calculate the final result - return result; - } - } // namespace modular_division -} // namespace math +/** + * @brief This function calculates modular division + * @param a integer dividend + * @param b integer divisor + * @param p integer modulo + * @return a/b modulo c + */ +uint64_t mod_division(uint64_t a, uint64_t b, uint64_t p) { + uint64_t inverse = power(b, p - 2, p) % p; /// Calculate the inverse of b + uint64_t result = + ((a % p) * (inverse % p)) % p; /// Calculate the final result + return result; +} +} // namespace modular_division +} // namespace math /** * Function for testing power function. @@ -107,6 +109,6 @@ static void test() { * @returns 0 on exit */ int main(int argc, char *argv[]) { - test(); // execute the tests + test(); // execute the tests return 0; } From 7293e15a306bf429ac41124981cb3289ae66b4a0 Mon Sep 17 00:00:00 2001 From: shubhamamsa Date: Mon, 18 Jan 2021 16:14:35 +0530 Subject: [PATCH 5/6] feat: addressed comments for math/modular_division.cpp --- math/modular_division.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/math/modular_division.cpp b/math/modular_division.cpp index 67001b8cf..89bbdf9bb 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -34,7 +34,8 @@ namespace math { /** * @namespace modular_division - * @brief Functions for Modular Division implementation + * @brief Functions for [Modular + * Division](https://www.geeksforgeeks.org/modular-division) implementation */ namespace modular_division { /** From a4f830b90be32c66645174a33fae3808e906cb3a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 18 Jan 2021 10:45:57 +0000 Subject: [PATCH 6/6] clang-format and clang-tidy fixes for 7293e15a --- math/modular_division.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/modular_division.cpp b/math/modular_division.cpp index 89bbdf9bb..6fd984f2b 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -34,7 +34,7 @@ namespace math { /** * @namespace modular_division - * @brief Functions for [Modular + * @brief Functions for [Modular * Division](https://www.geeksforgeeks.org/modular-division) implementation */ namespace modular_division {