理系学生日記

おまえはいつまで学生気分なのか

問題1.21

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

(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 (+ test-divisor 1)))))

(define (divides? a b)
  (= (remainder b a) 0))

このsmallest-divisor手続きを199,1999,19999それぞれについて計算するだけ。
199の最小除数は199
1999の最小除数は1999
19999の最小除数は7