Skip to content

Instantly share code, notes, and snippets.

@ch-hristov
Last active October 5, 2015 14:42
Show Gist options
  • Save ch-hristov/c73299cef34ee29b01f5 to your computer and use it in GitHub Desktop.
Save ch-hristov/c73299cef34ee29b01f5 to your computer and use it in GitHub Desktop.
exponentiation by squaring
#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