23 hours ago
my attempt to do the exercises in sicp.
Saturday, June 28, 2008
sicp exercise 1.21
; Exercise 1.21. Use the smallest-divisor procedure to find the smallest divisor of each of the
; following numbers: 199, 1999, 19999.
(define (smallest-divisor n)
(define (square number) (* number number))
(define (divides? n divisor) (= (remainder n divisor) 0))
(define (find-divisor n divisor)
(cond ((> (square divisor) n) n)
((divides? n divisor) divisor)
(else (find-divisor n (+ divisor 1)))))
(find-divisor n 2))
(display (smallest-divisor 10)) (newline)
(display (smallest-divisor 199)) (newline)
(display (smallest-divisor 1999)) (newline)
(display (smallest-divisor 19999)) (newline)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment