mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 14:36:25 +08:00
added binary_exponent.cpp
This commit is contained in:
25
math/binary_exponent.cpp
Normal file
25
math/binary_exponent.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/// C++ Program to find Binary Exponent recursively.
|
||||
|
||||
#include<iostream>
|
||||
/*
|
||||
* Calculating a^b in O(log(b)) by converting b in binary no.
|
||||
* Binary exponentiation (also known as exponentiation by squaring)
|
||||
* is a trick which allows to calculate an using only O(logn) multiplications
|
||||
* (instead of O(n) multiplications required by the naive approach).
|
||||
*/
|
||||
|
||||
int binExpo(int a,int b) {
|
||||
if (b == 0) return 1;
|
||||
int res = binExpo(a,b/2);
|
||||
if (b%2) return res*res*a;
|
||||
else return res*res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a,b;
|
||||
/// Give two nos as a^b (where '^' denotes power exponent operation
|
||||
std::cin >> a >> b;
|
||||
///Result of a^b
|
||||
std::cout << binExpo(a,b) << endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user