ili*_*rit 4 algorithm math discrete-mathematics
我现在使用的算法很快就会遇到极高的数字.算法中的一步我将x提升到应用于y的totient函数的结果.结果是你可能遇到很大的数字.
例如.当计算10模数53 的乘法顺序时:
10^totient(53) == 10^52 == 1 * 10^52
Run Code Online (Sandbox Code Playgroud)
以下算法在避免大数量方面表现更好,但在10 ^ mOrder大于数据类型容量的情况下仍然失败:
mOrder = 1
while 10^mOrder % 53 != 1
if mOrder >= i
mOrder = 0;
break
else
mOrder = mOrder + 1
Run Code Online (Sandbox Code Playgroud)
使用模幂运算,可以计算(10 ^ mOrder%53)或通常任何(a ^ b mod c)而不会得到比c大得多的值.有关详细信息,请参阅维基百科,此示例代码也是如此:
Bignum modpow(Bignum base, Bignum exponent, Bignum modulus) {
Bignum result = 1;
while (exponent > 0) {
if ((exponent & 1) == 1) {
// multiply in this bit's contribution while using modulus to keep result small
result = (result * base) % modulus;
}
// move to the next bit of the exponent, square (and mod) the base accordingly
exponent >>= 1;
base = (base * base) % modulus;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
为什么取幂?难道你不能在循环中乘以模数n吗?
(defun multiplicative-order (a n) (if (> (gcd a n) 1) 0 (do ((order 1 (+ order 1)) (mod-exp (mod a n) (mod (* mod-exp a) n))) ((= mod-exp 1) order))))
或者,在ptheudo(原文如此)代码中:
def multiplicative_order (a, n) :
if gcd (a, n) > 1 :
return 0
else:
order = 1
mod_exp = a mod n
while mod_exp != 1 :
order += 1
mod_exp = (mod_exp * a) mod n
return order
Run Code Online (Sandbox Code Playgroud)