From a092c7f67047c1279fd0479f545101abf59ea8ef Mon Sep 17 00:00:00 2001 From: NaiJi Date: Tue, 1 Sep 2020 18:01:47 +0300 Subject: [PATCH] Add 1.23 --- sicp-ex-1.23.rkt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sicp-ex-1.23.rkt diff --git a/sicp-ex-1.23.rkt b/sicp-ex-1.23.rkt new file mode 100644 index 0000000..3275568 --- /dev/null +++ b/sicp-ex-1.23.rkt @@ -0,0 +1,21 @@ +(define (square n) + (* n n)) + +(define (smallest-divisor n) + (find-divisor n 2)) + +(define (find-divisor n test-divisor) + (define (next iterator) + (if (= iterator 2) 3 (+ iterator 2))) + + (cond ((> (square test-divisor) n) n) + ((divides? test-divisor n) test-divisor) + (else (find-divisor n (next test-divisor))))) +; find-divisor + +(define (divides? a b) + (= (remainder b a) 0)) + +(smallest-divisor 199) +(smallest-divisor 1999) +(smallest-divisor 19999)