mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
clang-format and clang-tidy fixes for 2ad5420a
This commit is contained in:
parent
0e1e441ab3
commit
b9cdab9354
@ -1,11 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @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)
|
* 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
|
||||||
* [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
|
* Now, we can multiply the dividend with the inverse of divisor
|
||||||
* and modulo is distributive over multiplication operation.
|
* and modulo is distributive over multiplication operation.
|
||||||
* Let,
|
* Let,
|
||||||
@ -31,51 +32,52 @@
|
|||||||
* @brief Mathematical algorithms
|
* @brief Mathematical algorithms
|
||||||
*/
|
*/
|
||||||
namespace math {
|
namespace math {
|
||||||
/**
|
/**
|
||||||
* @namespace modular_division
|
* @namespace modular_division
|
||||||
* @brief Functions for Modular Division implementation
|
* @brief Functions for Modular Division implementation
|
||||||
*/
|
*/
|
||||||
namespace modular_division {
|
namespace modular_division {
|
||||||
/**
|
/**
|
||||||
* @brief This function calculates a raised to exponent b under modulo c using
|
* @brief This function calculates a raised to exponent b under modulo c using
|
||||||
* modular exponentiation.
|
* modular exponentiation.
|
||||||
* @param a integer base
|
* @param a integer base
|
||||||
* @param b unsigned integer exponent
|
* @param b unsigned integer exponent
|
||||||
* @param c integer modulo
|
* @param c integer modulo
|
||||||
* @return a raised to power b modulo c
|
* @return a raised to power b modulo c
|
||||||
*/
|
*/
|
||||||
uint64_t power(uint64_t a, uint64_t b, uint64_t c) {
|
uint64_t power(uint64_t a, uint64_t b, uint64_t c) {
|
||||||
uint64_t ans = 1; /// Initialize the answer to be returned
|
uint64_t ans = 1; /// Initialize the answer to be returned
|
||||||
a = a % c; /// Update a if it is more than or equal to c
|
a = a % c; /// Update a if it is more than or equal to c
|
||||||
if (a == 0) {
|
if (a == 0) {
|
||||||
return 0; /// In case a is divisible by c;
|
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;
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
*/
|
*/
|
||||||
uint64_t mod_division(uint64_t a, uint64_t b, uint64_t p) {
|
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 inverse = power(b, p - 2, p) % p; /// Calculate the inverse of b
|
||||||
uint64_t result = ((a%p)*(inverse%p))%p; /// Calculate the final result
|
uint64_t result =
|
||||||
return result;
|
((a % p) * (inverse % p)) % p; /// Calculate the final result
|
||||||
}
|
return result;
|
||||||
} // namespace modular_division
|
}
|
||||||
} // namespace math
|
} // namespace modular_division
|
||||||
|
} // namespace math
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function for testing power function.
|
* Function for testing power function.
|
||||||
@ -107,6 +109,6 @@ static void test() {
|
|||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
test(); // execute the tests
|
test(); // execute the tests
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user