Open Source WEB

##(link2sicp "book-Z-H-11.html#%_thm_1.27" "Exercise 1.27")

解答例

(define (fermat-test2 n)
  (define (try-it a)
    (= (expmod a n n) a))
  (define (iter a)
    (if (= a 1)
        #t
        (and (try-it a)
             (iter (- a 1)))))
  (iter (- n 1)))

実行結果(fermat-test2 は素数と判定した場合に #t を返す)

gosh> (fermat-test2 561)
#t
gosh> (fermat-test2 1105)
#t
gosh> (fermat-test2 1729)
#t
gosh> (fermat-test2 2465)
#t
gosh> (fermat-test2 2821)
#t
gosh> (fermat-test2 6601)
#t

コード

##(sicp-answer-code "ex-1.27.scm")

このサイトは、 IPA の「平成15年度オープンソフトウエア活用基盤整備事業」 の委託事業として開発されたKahuaで試験的に運用しております。

Copyright (c) 2004-2007 株式会社タイムインターメディア About Us