Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Last active January 28, 2018 12:15
Show Gist options
  • Save vadimkorr/381db55ddd52f79ddee32c2b187bfa9e to your computer and use it in GitHub Desktop.
Save vadimkorr/381db55ddd52f79ddee32c2b187bfa9e to your computer and use it in GitHub Desktop.
Find x^y mod p
// Find x^y mod p
// Right-to-left method
let modPow = (x, y, p) => {
let res = 1;
x %= p;
while(y>0) {
if (y%2 == 1) res = res*x % p
y = Math.floor(y/2)
x = x*x % p
}
return res;
}
console.log(modPow(50, 100, 13)) // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment