是否有算法计算x模y(对于y <1000)的乘法顺序,不需要BigInteger类型?

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)

sch*_*der 7

使用模幂运算,可以计算(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)

  • 我不敢相信我忘记了这一点,特别是考虑到我两天前使用过它。我想我需要喝点咖啡。 (2认同)
  • 或者也许睡一会儿,然后喝杯咖啡;) (2认同)

Sva*_*nte 6

为什么取幂?难道你不能在循环中乘以模数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)