Skip to content

Commit 1d1a320

Browse files
authored
Merge pull request kothariji#522 from Yatindra29/master
added the fast power function, which calculates x^n in O(log n) time
2 parents 3d13f40 + f54b4ed commit 1d1a320

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<iostream>
2+
using namespace std;
3+
int fast_power(int num,int power){
4+
if(power==0){
5+
return 1;
6+
}
7+
int p=fast_power(num,power/2);
8+
p*=p;
9+
if(power&1){
10+
return num*p;
11+
}
12+
return p;
13+
}
14+
int main(){
15+
int num,power;
16+
cin>>num>>power;
17+
int p=fast_power(num,power);
18+
cout<<p<<endl;
19+
}

0 commit comments

Comments
 (0)