From ea5b18d44ad4fe148d76e6675fbd5038b06cadf8 Mon Sep 17 00:00:00 2001 From: ZARIX <46120048+jordanwww@users.noreply.github.com> Date: Wed, 6 Sep 2023 01:28:05 +0530 Subject: [PATCH] Create Binary Exponentiation --- .../Binary Exponentiation | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 M-MathematicalAlgorithms/Binary Exponentiation diff --git a/M-MathematicalAlgorithms/Binary Exponentiation b/M-MathematicalAlgorithms/Binary Exponentiation new file mode 100644 index 000000000..aafae766f --- /dev/null +++ b/M-MathematicalAlgorithms/Binary Exponentiation @@ -0,0 +1,32 @@ +#include +using namespace std ; + +// Binary Exponentiation .. + long long binpow(long long a, long long b) { + + long long res = 1; + while (b > 0) { + if (b & 1) + res = res * a; + a = a * a ; + b >>= 1; + } + return res; + } + +int main(){ +int num , pow ; +cout << "Enter the Number = " << "\n" ; +cin >> num ; +cout << "Enter the Power = " << "\n" ; +cin >> pow ; +long long res = binpow(num,pow); +cout << "Result = " << res ; + +} +Time Complexity :- O(logn) +Space Complexity :- O(1) +Test case :- +2 4 +Result :- +16