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 4b93d5a5
This commit is contained in:
parent
bfa9eef130
commit
0c03970f10
@ -17,26 +17,21 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
/// Iterative Function to calculate a raised to exponent b
|
/// Iterative Function to calculate a raised to exponent b
|
||||||
/// under modulo c in O(log b) using modular exponentiation.
|
/// under modulo c in O(log b) using modular exponentiation.
|
||||||
int power(int a, unsigned int b, int c)
|
int power(int a, unsigned int b, int c) {
|
||||||
{
|
|
||||||
int ans = 1; /// Initialize the answer to be returned
|
int ans = 1; /// Initialize the answer to be returned
|
||||||
|
|
||||||
a = a % c; /// Update a if it is more than or
|
a = a % c; /// Update a if it is more than or
|
||||||
/// equal to c
|
/// 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)
|
while (b > 0) {
|
||||||
{
|
|
||||||
/// If b is odd, multiply a with answer
|
/// If b is odd, multiply a with answer
|
||||||
if (b & 1)
|
if (b & 1) {
|
||||||
{
|
|
||||||
ans = (ans * a) % c;
|
ans = (ans * a) % c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,16 +47,14 @@ int power(int a, unsigned int b, int c)
|
|||||||
* @brief Main function
|
* @brief Main function
|
||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
/// Give two numbers num1, num2 and modulo m
|
/// Give two numbers num1, num2 and modulo m
|
||||||
int num1 = 2;
|
int num1 = 2;
|
||||||
int num2 = 5;
|
int num2 = 5;
|
||||||
int m = 13;
|
int m = 13;
|
||||||
|
|
||||||
std::cout << "The value of "<<num1<<" raised to exponent "<<num2<<
|
std::cout << "The value of " << num1 << " raised to exponent " << num2
|
||||||
" under modulo "<<m<<" is " << power(num1, num2, m);
|
<< " under modulo " << m << " is " << power(num1, num2, m);
|
||||||
/// std::cout << "The value of "<<num1<<" raised to exponent "<<num2<<"
|
/// std::cout << "The value of "<<num1<<" raised to exponent "<<num2<<"
|
||||||
/// " under modulo "<<m<<" is " << power(num1, num2, m);
|
/// " under modulo "<<m<<" is " << power(num1, num2, m);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user