Skip to content

Instantly share code, notes, and snippets.

@MattiasFestin
Last active August 29, 2015 14:20
Show Gist options
  • Save MattiasFestin/6e688fef1174f7bc6fc3 to your computer and use it in GitHub Desktop.
Save MattiasFestin/6e688fef1174f7bc6fc3 to your computer and use it in GitHub Desktop.
Power modulo (Requires propper tail calls)
let modPow = (b, e, m, value = 1) => {
b = b % m;
if (e % 2 === 1) {
value = (value * b) % m;
}
return e <= 1 ? value : modPow(b * b, e >> 1, m, value);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment