Skip to content

Commit

Permalink
chore(CPlusPlus): add binary power on maths algorithm (MakeContributi…
Browse files Browse the repository at this point in the history
…ons#810)

Co-authored-by: angshudas <[email protected]>
Co-authored-by: Mohnish Deshpande <[email protected]>
  • Loading branch information
3 people authored Sep 1, 2022
1 parent a68c012 commit 88b0e15
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions algorithms/CPlusPlus/Maths/binary-power.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
1 change: 1 addition & 0 deletions algorithms/CPlusPlus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
- [Power of two](Maths/power-of-two.cpp)
- [Small numbers](Maths/small-numbers.cpp)
- [Segmented Sieve](Maths/segmented-sieve-range.cpp)
- [Binary Power](Maths/binary-power.cpp)

# Recursion

Expand Down

0 comments on commit 88b0e15

Please sign in to comment.