Skip to content

Instantly share code, notes, and snippets.

@lnfnunes
Last active January 23, 2017 22:15
Show Gist options
  • Save lnfnunes/cfb8939373ce993824c58ef16439c19b to your computer and use it in GitHub Desktop.
Save lnfnunes/cfb8939373ce993824c58ef16439c19b to your computer and use it in GitHub Desktop.
HackerRank helper (math) functions
/**
* Fast modular exponentiation for i ^ n % modulus
* Used when exp is a extremely large number, ie: Math.pow(2, 400);
* @returns {number}
*/
function fastModularExponentiation(i, n, modulus) {
if (modulus == 1) return 0;
var c = 1;
for (var e = 1; e <= n; e++) {
c = (c * i) % modulus;
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment