-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-power.cpp
56 lines (48 loc) · 1.04 KB
/
binary-power.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
/*
The idea is to half the power on every iteration
Computes the power in log(n) operations
On every iteration:
Square the base, half the power
Special case - if power is odd:
Multiply the ans with a
Because the ODD-NUMBER % 2 == 1
Note: There will always be one iteration where power is odd
*/
long binpow(long a, long b){
long ans=1;
// while b is greater than zero, we continue the binary exponentiation
while(b>0){
// if b is odd, multiply result with base
if(b&1)
ans *= a;
// square the base
a *= a;
// half the power
b /= 2;
}
return ans;
}
// source: @angshudas
long binpow_rec(long a, long b){
// a^0 = 1 [for any a]
if(b==0)
return 1;
// recursive call for a^(b/2)
long ans = binpow_rec(a, b/2);
// square the result
ans *= ans;
// if b is odd, times by a
// to cover for
if(b&1)
ans *= a;
return ans;
}
int main(){
long base, power;
cout<<"Enter the base and power: "<<endl;
cin>>base>>power;
cout<<base<<" ^ "<<power<<" = "<<binpow(base, power)<<endl;
return 0;
}