Skip to content

Instantly share code, notes, and snippets.

@chul-hyun
Created October 11, 2016 16:38
Show Gist options
  • Save chul-hyun/88b01f5d86f22886981a9d6ecd9d662e to your computer and use it in GitHub Desktop.
Save chul-hyun/88b01f5d86f22886981a9d6ecd9d662e to your computer and use it in GitHub Desktop.
고속 누승 알고리즘
/**
* 고속 누승 알고리즘 구현.
* x^p mod(m) 계산
* 참고: http://a.nex.kr.pe/wordpress/2015/10/21/제-6강-고속-누승-알고리즘과-모듈러/
* @method powMod
* @param {int} x 피제수
* @param {int} p 지수
* @param {int} m 제수
* @return {int} x^p mod(m) 계산 결과값
*/
function powMod(a, b, n){
let result = 1;
while(b)
{
if(b & 1)
{
result = (result * a) % n ;
}
b = parseInt(b / 2);
a = (a * a) % n ;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment