gpt4 book ai didi

algorithm - SICP练习1.28 : false negatives in the Miller-Rabin test

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:30:20 24 4
gpt4 key购买 nike

Exercise 1.28. One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat's Little Theorem, which states that if n is a prime number and a is any positive integer less than n, then a raised to the (n - 1)st power is congruent to 1 modulo n. To test the primality of a number n by the Miller-Rabin test, we pick a random number a < n and raise a to the (n - 1)st power modulo n using the expmod procedure. However, whenever we perform the squaring step in expmod, we check to see if we have discovered a ''nontrivial square root of 1 modulo n,'' that is, a number not equal to 1 or n - 1 whose square is equal to 1 modulo n. It is possible to prove that if such a nontrivial square root of 1 exists, then n is not prime. It is also possible to prove that if n is an odd number that is not prime, then, for at least half the numbers a < n, computing a^(n-1) in this way will reveal a nontrivial square root of 1 modulo n. (This is why the Miller-Rabin test cannot be fooled.) Modify the expmod procedure to signal if it discovers a nontrivial square root of 1, and use this to implement the Miller-Rabin test with a procedure analogous to fermat-test. Check your procedure by testing various known primes and non-primes. Hint: One convenient way to make expmod signal is to have it return 0.

(define (fast-prime? n)
(define (fast-prime-iter n counter)
(cond ((= counter 1) #t) ; There is no need to check 1
((miller-rabin-test n counter)
(fast-prime-iter n (- counter 1)))
(else
(newline)
(display counter)
#f)))
(fast-prime-iter n (- n 2)))

(define (miller-rabin-test n a)
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp)
(nontrivial-square-root?
(remainder (square (expmod base (/ exp 2) m))
m)))
(else
(remainder (* base (expmod base (- exp 1) m))
m))))
(= (expmod a (- n 1) n) 1))

(define (nontrivial-square-root? val)
(if (= val 1)
0
val))

我的想法是使用过程 nontrivial-square-root? 过滤掉那些所谓的“1 模 n 的非平凡平方根”。如果 (remainder (square (expmod base (/exp 2) m)) m) 为 1,则返回 0,在这种情况下,(expmod base (/exp 2) m) 必须等于 1 modulo n(这是因为 m 始终等于 n),使其成为非平凡的平方根。

虽然 nontrivial-square-root? 确实过滤掉了 561、1105、1729、2465、2821 和 6601 等卡迈克尔数,但据报道 7 和 13 等素数也是合数。

是什么导致了这些假阴性?

最佳答案

引用的重要部分用粗体标出:

However, whenever we perform the squaring step in expmod, we check to see if we have discovered a ''nontrivial square root of 1 modulo n,'' that is, a number not equal to 1 or n - 1 whose square is equal to 1 modulo n

因此,在平方和取余之前,您必须检查参数不是 1 或 n - 1。这种情况会发生,例如,如果您调用 (miller-rabin-test 5 3) .计算递归时,您会注意到有一个调用 (nontrivial-square-root? (remainder (square 4) 5)) 计算结果为 (nontrivial-square-root? 1)。但是,5 仍然可以是质数,因为 4 是 5 - 1。

因此在平方部分,您可以调用以下函数:

(define (sqrmod-with-check val n)
(let ((sqrmod (remainder (square val) n)))
(cond ((or (= val (- n 1)) (= val 1)) sqrmod)
((= sqrmod 1) 0)
(else sqrmod))))

参数是 expmod 调用和 m。这会为您计算平方和余数,除非我们找到 1 模 n 的非平凡平方根,当它返回 0 时。我将它分为三个条件,而不是两个,只是为了可读性。

关于algorithm - SICP练习1.28 : false negatives in the Miller-Rabin test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com