Skip to content

Instantly share code, notes, and snippets.

@lnikon
Created August 8, 2018 07:35
Show Gist options
  • Save lnikon/3caf18b3a6f8d4b602830aa1f8b63901 to your computer and use it in GitHub Desktop.
Save lnikon/3caf18b3a6f8d4b602830aa1f8b63901 to your computer and use it in GitHub Desktop.
Binary Power
#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