Created
October 11, 2016 16:38
-
-
Save chul-hyun/88b01f5d86f22886981a9d6ecd9d662e to your computer and use it in GitHub Desktop.
고속 누승 알고리즘
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
/** | |
* 고속 누승 알고리즘 구현. | |
* 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