summaryrefslogtreecommitdiff
path: root/coding-exercises/1/21.rkt
blob: 7cda52fa491e5ff45939d3544965d075ad798310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#lang racket
(require racket/pretty)
(require sicp)

(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 (square x) (* x x))

(define (divides? a b)
  (= (remainder b a) 0))

;; (pretty-print (smallest-divisor 199))
;; (pretty-print (smallest-divisor 1999))
;; (pretty-print (smallest-divisor 19999))