Professional Documents
Culture Documents
1. Aim/Overview of the practical: Code to implement power function in O(log n) time complexity.
The algorithm is simple implementation of following recurrence relation used to calculate 'a' to
the power 'b' where 'a' and 'b' are integers.
4. Steps for experiment/practical/Code:
#include<iostream>
using namespace std;
class pow
{
public:
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
};
int main()
{
pow g;
int x = 4;
unsigned int y = 5;