Skip to content

Instantly share code, notes, and snippets.

@910JQK
Last active August 29, 2015 14:06
Show Gist options
  • Save 910JQK/f3e5852ac043f0a2f9ec to your computer and use it in GitHub Desktop.
Save 910JQK/f3e5852ac043f0a2f9ec to your computer and use it in GitHub Desktop.
[140913]Power
#include <iostream>
typedef unsigned int uint;
using std::cin;
using std::cout;
uint power(uint a, uint n){
if(a == 1)
return 1;
if(a != 0 && n == 0)
return 1;
if(a == 0 && n == 0)
throw "invalid argument";
uint t, r;
t = a;
r = 1;
while(n > 0){
if(n % 2 == 1)
r *= t;
n /= 2;
t *= t;
}
return r;
}
int main(int argc, char *argv[]){
uint a, n;
cin >> a >> n;
cout << power(a, n) << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment