sicp 1.28

Miller-Rabin检查:

 

(define (fast-prime? n a)
  (cond ((= a n) #t)
        ((miller-rabin-test n a) (fast-prime? n (+ a 1)))
        (else #f)))

(define (miller-rabin-test n a)
  (= (expmod a (- n 1) n) 1))

(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
         (let ((tmp (expmod base (/ exp 2) m)))
         (if (and (or (not (= tmp 1)) (not (= tmp (- m 1)))) (= (square tmp) 1))
             0
             (remainder (square tmp) m))))
        (else (remainder (* base (expmod base (- exp 1) m)) m))))

(define (square x)
  (* x x))

你可能感兴趣的:(F#)