Created
August 8, 2018 07:35
-
-
Save lnikon/3caf18b3a6f8d4b602830aa1f8b63901 to your computer and use it in GitHub Desktop.
Binary Power
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
using ull = unsigned long long; | |
ull fast_power(ull num, int power) | |
{ | |
if(num < 0 || power < 0) { | |
std::cout << "invalid arguments specified\n"; | |
} | |
int k = power; | |
ull b = 1; | |
ull c = num; | |
while(k != 0) | |
{ | |
if (k % 2 == 0) | |
{ | |
k /= 2; | |
c *= c; | |
} | |
else | |
{ | |
k--; | |
b *= c; | |
} | |
} | |
std::cout << "b = " << b << "\nc = " << c << "\n"; | |
} | |
int main() | |
{ | |
fast_power(45, 6); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment