Skip to content

Instantly share code, notes, and snippets.

@f1729
Created September 18, 2018 03:22
Show Gist options
  • Save f1729/fe3dc1a2983bfc76cbae568ff1f756c3 to your computer and use it in GitHub Desktop.
Save f1729/fe3dc1a2983bfc76cbae568ff1f756c3 to your computer and use it in GitHub Desktop.
function power(x , y) {
var base = x;
if ( y < 0 ) {
base = 1 / x;
y = -1*y;
}
var coeff = 1;
while (y > 1) {
if (y%2 == 0) {
base *= base;
y = Math.floor(y / 2);
}
else {
coeff *= base;
base *= base;
y = Math.floor((y - 1) / 2);
}
}
return coeff * base
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment