大数的快速幂取模JavaScript实现

时间:2022-05-27 22:38:58   收藏:0   阅读:11
/**
 * 计算a**b(mod c)
 * @param {bigInt} a 底数
 * @param {bigInt} b 指数
 * @param {bigInt} c 模
 * @returns {binInt}结果
 */
function fastPowerMod(a, b, c){
    let res = 1n 
    a %= c

    while (b) {
        if(b&1n){
            res = (res*a)%c
        }
        a = a*a%c
        b>>=1n
    }
    return res
}

原文:https://www.cnblogs.com/pangqianjin/p/15357861.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!