Last active
May 22, 2020 20:46
-
-
Save alonat/070169bda39eafa12c6e7d3abe467f97 to your computer and use it in GitHub Desktop.
This file contains 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
const binpowCount = () => { | |
let i = 0; | |
return function binpow (a, b) { | |
i++ | |
if (b === 1) { | |
console.log(i) | |
return a; | |
} | |
return b & 1 ? a * binpow(a, b - 1) : binpow(a, b >> 1 ) ** 2 | |
} | |
} | |
const binpow1 = binpowCount() | |
const binpow2 = binpowCount() | |
const binpow3 = binpowCount() | |
const binpow4 = binpowCount() | |
const binpow5 = binpowCount() | |
console.assert(binpow1(2, 3) === 8); | |
console.assert(binpow2(5, 7) === 78125); | |
console.assert(binpow3(6, 8) === 1679616); | |
console.assert(binpow4(2, 32) === 4294967296); | |
console.assert(binpow5(2, 52) === 4503599627370496); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment