-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1-21.scm
38 lines (29 loc) · 901 Bytes
/
ex1-21.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
; “Use the smallest-divisor procedure to find the smallest divisor of each of the
; following numbers: 199, 1999, 19999.”
(define (find-smallest-divisor num)
(define (find-smallest-divisor-iter num smallest-divisor)
(cond ((> (square smallest-divisor) num) num)
((zero? (remainder num smallest-divisor)) smallest-divisor)
(else (find-smallest-divisor-iter num (next smallest-divisor)))
)
)
(find-smallest-divisor-iter num 2)
)
(define (square x)
(* x x)
)
(define (next num)
(if (= num 2)
3
(+ num 2)
)
)
(display "find-smallest-divisor: 199 => \n")
(display (find-smallest-divisor 199))
(display "\n")
(display "find-smallest-divisor: 1999 =>\n")
(display (find-smallest-divisor 1999))
(display "\n")
(display "find-smallest-divisor: 19999 =>\n")
(display (find-smallest-divisor 19999))
(display "\n")