Created
May 16, 2015 23:51
-
-
Save krzkaczor/0bdba0ee9555659ae5fe to your computer and use it in GitHub Desktop.
Fast modular exponentiation in Java Script
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 a ^ b mod n | |
* @returns {number} | |
*/ | |
var fastModularExponentiation = function(a, b, n) { | |
a = a % n; | |
var result = 1; | |
var x = a; | |
while(b > 0){ | |
var leastSignificantBit = b % 2; | |
b = Math.floor(b / 2); | |
if (leastSignificantBit == 1) { | |
result = result * x; | |
result = result % n; | |
} | |
x = x * x; | |
x = x % n; | |
} | |
return result; | |
}; | |
var assert = function(actual, expected){ | |
if (actual != expected){ | |
throw new Error('Assertion failed'); | |
} | |
}; | |
assert(fastModularExponentiation(12, 53, 7), 3); | |
assert(fastModularExponentiation(7, 12, 10), 1); | |
assert(fastModularExponentiation(3, 51, 13), 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@y-richie-y @devildelta Now that chrome and node.js support
BigInt
natively, you can use javascript as well.Simply append an
n
to every number literal to use BigInt: