mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-10 08:03:30 +08:00
Update modular_exponentiation.cpp
This commit is contained in:
committed by
GitHub
parent
0c03970f10
commit
d8f2001fd3
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief C++ Program for Modular Exponentiation Iteratively.
|
||||
* Calculate the value of an integer a raised to an integer exponent b
|
||||
* under modulo c.
|
||||
* Calculate the value of an integer a raised to an integer exponent b
|
||||
* under modulo c.
|
||||
* @note The time complexity of this approach is O(log b).
|
||||
*
|
||||
* Example:
|
||||
@@ -15,46 +15,60 @@
|
||||
* We can also verify the result as 4^3 is 64 and 64 modulo 5 is 4
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/// Iterative Function to calculate a raised to exponent b
|
||||
/// under modulo c in O(log b) using modular exponentiation.
|
||||
int power(int a, unsigned 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 * a) % c;
|
||||
}
|
||||
|
||||
/// b must be even now
|
||||
b = b >> 1; // b = b/2
|
||||
a = (a * a) % c;
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
/// Give two numbers num1, num2 and modulo m
|
||||
int num1 = 2;
|
||||
int num2 = 5;
|
||||
int m = 13;
|
||||
* @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, unsigned 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*a) % c;
|
||||
}
|
||||
|
||||
/// b must be even now
|
||||
b = b>>1; // b = b/2
|
||||
a = (a*a) % c;
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
/// Give two numbers num1, num2 and modulo m
|
||||
int num1 = 2;
|
||||
int num2 = 5;
|
||||
int m = 13;
|
||||
|
||||
std::cout << "The value of "<<num1<<" raised to exponent "<<num2<<
|
||||
" under modulo "<<m<<" is " << power(num1, num2, m);
|
||||
/// std::cout << "The value of "<<num1<<" raised to exponent "<<num2<<"
|
||||
/// " under modulo "<<m<<" is " << power(num1, num2, m);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "The value of " << num1 << " raised to exponent " << num2
|
||||
<< " under modulo " << m << " is " << power(num1, num2, m);
|
||||
/// std::cout << "The value of "<<num1<<" raised to exponent "<<num2<<"
|
||||
/// " under modulo "<<m<<" is " << power(num1, num2, m);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user