summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2017-06-17 13:02:23 +0000
committerrsiddharth <s@ricketyspace.net>2017-06-17 13:02:23 +0000
commitd4deb2fc72cd436a874987c67567c104e71c3608 (patch)
treee52f91a2639fb40baaa9cf6b3d1859478da6d6e2
parent246359ab7964855b02d7994588d8df10541ab599 (diff)
net: Add module (net ricketyspace sicp one twentyone).
* net/ricketyspace/sicp/one/twentyone.scm: New file.
-rw-r--r--net/ricketyspace/sicp/one/twentyone.scm42
1 files changed, 42 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/twentyone.scm b/net/ricketyspace/sicp/one/twentyone.scm
new file mode 100644
index 0000000..9d1c94f
--- /dev/null
+++ b/net/ricketyspace/sicp/one/twentyone.scm
@@ -0,0 +1,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
+;;;