Last active
January 23, 2017 22:15
-
-
Save lnfnunes/cfb8939373ce993824c58ef16439c19b to your computer and use it in GitHub Desktop.
HackerRank helper (math) functions
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
/** | |
* 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