mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
feat: Addressed comments for adding modular division algorithm
This commit is contained in:
parent
0450f6a179
commit
2ad5420a7c
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @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
|
* @details To calculate division of two numbers under modulo p
|
||||||
* Modulo operator is not distributive under division, therefore
|
* Modulo operator is not distributive under division, therefore
|
||||||
* we first have to calculate the inverse of divisor using
|
* 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
|
* NOTE: For the existence of inverse of 'b', 'b' and 'p' must be coprime
|
||||||
* For simplicity we take p as prime
|
* For simplicity we take p as prime
|
||||||
* Time Complexity: O(log(b))
|
* Time Complexity: O(log(b))
|
||||||
*
|
|
||||||
* Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 --- (i)
|
* Example: ( 24 / 3 ) % 5 => 8 % 5 = 3 --- (i)
|
||||||
Now the inverse of 3 is 2
|
Now the inverse of 3 is 2
|
||||||
(24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 --- (ii)
|
(24 * 2) % 5 = (24 % 5) * (2 % 5) = (4 * 2) % 5 = 3 --- (ii)
|
||||||
(i) and (ii) are equal hence the answer is correct.
|
(i) and (ii) are equal hence the answer is correct.
|
||||||
|
|
||||||
*
|
|
||||||
* @see modular_inverse_fermat_little_theorem.cpp, modular_exponentiation.cpp
|
* @see modular_inverse_fermat_little_theorem.cpp, modular_exponentiation.cpp
|
||||||
*
|
|
||||||
* @author [Shubham Yadav](https://github.com/shubhamamsa)
|
* @author [Shubham Yadav](https://github.com/shubhamamsa)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert> /// for assert
|
||||||
#include <iostream>
|
#include <iostream> /// for IO operations
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace math
|
* @namespace math
|
||||||
|
* @brief Mathematical algorithms
|
||||||
*/
|
*/
|
||||||
namespace math {
|
namespace math {
|
||||||
/**
|
/**
|
||||||
* @brief This function calculates a raised to exponent b under modulo c using
|
* @namespace modular_division
|
||||||
* modular exponentiation.
|
* @brief Functions for Modular Division implementation
|
||||||
* @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) {
|
namespace modular_division {
|
||||||
int ans = 1; /// Initialize the answer to be returned
|
/**
|
||||||
a = a % c; /// Update a if it is more than or equal to c
|
* @brief This function calculates a raised to exponent b under modulo c using
|
||||||
if (a == 0) {
|
* modular exponentiation.
|
||||||
return 0; /// In case a is divisible by c;
|
* @param a integer base
|
||||||
}
|
* @param b unsigned integer exponent
|
||||||
while (b > 0) {
|
* @param c integer modulo
|
||||||
/// If b is odd, multiply a with answer
|
* @return a raised to power b modulo c
|
||||||
if (b & 1) {
|
*/
|
||||||
ans = ((ans % c) * (a % c)) % c;
|
uint64_t power(uint64_t a, uint64_t b, uint64_t c) {
|
||||||
}
|
uint64_t ans = 1; /// Initialize the answer to be returned
|
||||||
/// b must be even now
|
a = a % c; /// Update a if it is more than or equal to c
|
||||||
b = b >> 1; /// b = b/2
|
if (a == 0) {
|
||||||
a = ((a % c) * (a % c)) % c;
|
return 0; /// In case a is divisible by c;
|
||||||
}
|
}
|
||||||
return ans;
|
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
|
* @brief This function calculates modular division
|
||||||
* @param a integer dividend
|
* @param a integer dividend
|
||||||
* @param b integer divisor
|
* @param b integer divisor
|
||||||
* @param p integer modulo
|
* @param p integer modulo
|
||||||
* @return a/b modulo c
|
* @return a/b modulo c
|
||||||
*/
|
*/
|
||||||
int mod_division(int a, int b, int p) {
|
uint64_t mod_division(uint64_t a, uint64_t b, uint64_t p) {
|
||||||
int inverse = power(b, p-2, p)%p; /// Calculate the inverse of b
|
uint64_t inverse = power(b, p-2, p)%p; /// Calculate the inverse of b
|
||||||
int result = ((a%p)*(inverse%p))%p; /// Calculate the final result
|
uint64_t result = ((a%p)*(inverse%p))%p; /// Calculate the final result
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
} // namespace modular_division
|
||||||
|
} // namespace math
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function for testing power function.
|
* Function for testing power function.
|
||||||
@ -80,19 +83,19 @@ namespace math {
|
|||||||
* @returns `void`
|
* @returns `void`
|
||||||
*/
|
*/
|
||||||
static void test() {
|
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);
|
assert(test_case_1 == 0);
|
||||||
std::cout << "Test 1 Passed!" << std::endl;
|
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);
|
assert(test_case_2 == 5);
|
||||||
std::cout << "Test 2 Passed!" << std::endl;
|
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);
|
assert(test_case_3 == 0);
|
||||||
std::cout << "Test 3 Passed!" << std::endl;
|
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);
|
assert(test_case_4 == 2);
|
||||||
std::cout << "Test 4 Passed!" << std::endl;
|
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);
|
assert(test_case_5 == 2);
|
||||||
std::cout << "Test 5 Passed!" << std::endl;
|
std::cout << "Test 5 Passed!" << std::endl;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user