Created
September 18, 2018 03:22
-
-
Save f1729/fe3dc1a2983bfc76cbae568ff1f756c3 to your computer and use it in GitHub Desktop.
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
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