power function Algorithm
In that case, bn is named" b raised to the n-th power"," b raised to the power of N"," the n-th power of b"," b to the N-th", or most briefly as" b to the n". exponentiation is used extensively in many fields, including economics, biology, chemistry, physics, and computer science, with applications such as compound interest, population increase, chemical reaction kinetics, wave behavior, and public-key cryptanalysis.
/*
* Implement pow(a, b) function.
* We can recursively call a sub-problem
* if n is even, then pow(a,b) = pow(a, b/2) * pow(a, b/2);
* if n is odd then, pow(a, b) = a * pow(a, b/2) * pow(a, b/2);
*/
#include <iostream>
int power(int a, int b)
{
// base condition
//
if (b == 0)
{
return 1;
}
// sub-problem for this recursive call.
//
int p = power(a, b/2);
// if b is odd
if (b & 1)
{
return (a * p * p);
}
else
{
// if b is even
return (p * p);
}
}
int main()
{
int a, b;
std::cout << "Calculating pow(a,b):\n";
std::cout << "Enter a:";
std::cin >> a;
std::cout << "Enter b:";
std::cin >> b;
std::cout << "pow(a,b) :" << power(a, b) << std::endl;
}