Last active
October 5, 2015 14:42
-
-
Save ch-hristov/c73299cef34ee29b01f5 to your computer and use it in GitHub Desktop.
exponentiation by squaring
This file contains hidden or 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 <iostream> | |
#include <cmath> | |
using namespace std; | |
int compute(int n,int exp){ | |
if(exp==1)return n; | |
int r = exp % 2; | |
if(r) return compute(n*n,exp >> 1) * n; | |
else compute(n*n,exp >> 1); | |
} | |
int main(){ | |
int number,exp; | |
cin >> number >> exp; | |
int z = compute(number,exp); | |
cout << z << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment