diff options
-rw-r--r-- | net/ricketyspace/sicp/one/twentythree.scm | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/twentythree.scm b/net/ricketyspace/sicp/one/twentythree.scm new file mode 100644 index 0000000..f88b05d --- /dev/null +++ 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)) |