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) 
  (find-divisor n 2))
(define (square x) (* x x))
(define (remainder a b) (cond ((< (- a b) b) (- a b))
                              (else (remainder (- a b) b))
                              ))
(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))
x
 
#<undef>
 
(smallest-divisor 199)
 
199
 
(smallest-divisor 1999)
 
1999
 
(smallest-divisor 19999)
 
7

results matching ""

    No results matching ""