To be noted: the drawing in exercise 1.14 is unfinished. I did it in a notebook, but haven't yet had the time to put it in a txt file.
18 lines
No EOL
414 B
Scheme
18 lines
No EOL
414 B
Scheme
#lang sicp
|
|
|
|
(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)
|
|
;199
|
|
;> (smallest-divisor 1999)
|
|
;1999
|
|
;> (smallest-divisor 19999)
|
|
;7 |