Calculate power of an element(x,y) where x is base and y is power in O(log(y)) times.with O(1) space complexity without using DIVIDE & Conquer !!
Calculate the power of an element(x,y) where x is base and y is power in O(log(y)) times. with O(1) space complexity without using DIVIDE & Conquer !!
C++ program for the above approach
Follow me on :
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int x = 2; //base
int y = 5; //power i.e x^y
int s = x;
for (int i = 1; i <= int (log2 (y)); i++)
{
s = s*s;
}
if(y % 2 == 0)
cout << x;
else
cout << x * s;
}
// code end//
// This is code is contributed //
// by Ayushshuklas
my website : ayushshuklas.netlify.com
Comments
Post a Comment