Skip to content

Instantly share code, notes, and snippets.

@alonat
Last active May 22, 2020 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonat/070169bda39eafa12c6e7d3abe467f97 to your computer and use it in GitHub Desktop.
Save alonat/070169bda39eafa12c6e7d3abe467f97 to your computer and use it in GitHub Desktop.
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