SICP习题1.23解答

(define (smallest-divisor n)
  (find-divisor n 2))
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (next test-divisor)))))
(define (divides? a b)
  (= (remainder b a) 0))
(define (square x)
  (* x x))

(define (next test-divisor)
  (if (= test-divisor 2)
      (+ test-divisor 1)
      (+ test-divisor 2)))

(define (prime? n)
  (= n (smallest-divisor n)))
posted @ 2006-12-27 21:46  浅蓝の天   阅读(114)  评论(0编辑  收藏  举报