sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit 30535ec1f959d828241b0f9863709587094ddeec
parent ab2ecb49ace565beb3f1393a59da7b7e3b399828
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 15 Jul 2017 22:19:51 +0000

net: Add (net ricketyspace sicp one twentythree).

* net/ricketyspace/sicp/one/twentythree.scm
(net ricketyspace sicp one twentythree): Add module.

Diffstat:
net/ricketyspace/sicp/one/twentythree.scm | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/net/ricketyspace/sicp/one/twentythree.scm b/net/ricketyspace/sicp/one/twentythree.scm @@ -0,0 +1,25 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (net ricketyspace sicp one twentythree) + #:use-module (net ricketyspace sicp utils) + #:export (smallest-divisor)) + +(define (square x) + (expt x 2)) + +(define (divides? a b) + (= (remainder b a) 0)) + +(define (next n) + (cond ((= n 2) 3) + (else (+ n 2)))) + +(define (find-divisor n test-divisor) + (cond ((> (square test-divisor) n) n) + ((divides? test-divisor n) test-divisor) + (else (find-divisor n (next test-divisor))))) + +(define (smallest-divisor n) + (find-divisor n 2))