summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/one/twentyone.scm
blob: 9d1c94f14064652c5996a6a13ada60dcf21c6cbd (plain) (blame)
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
39
40
41
42
;;;; Under Creative Commons Attribution-ShareAlike 4.0
;;;; International. See
;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.

(define-module (net ricketyspace sicp one twentyone)
  #:export (sm-divisor-of-199
            sm-divisor-of-1999
            sm-divisor-of-19999))

(define (small-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))

(define (square x)
  (expt x 2))

(define (sm-divisor-of-199)
  (small-divisor 199))

(define (sm-divisor-of-1999)
  (small-divisor 1999))

(define (sm-divisor-of-19999)
  (small-divisor 19999))

;;;
;;; scheme@(guile-user)> ,use (net ricketyspace sicp one twentyone)
;;; ...
;;; scheme@(guile-user)> (sm-divisor-of-199)
;;; $3 = 199
;;; scheme@(guile-user)> (sm-divisor-of-1999)
;;; $4 = 1999
;;; scheme@(guile-user)> (sm-divisor-of-19999)
;;; $5 = 7
;;;