summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2017-06-24 21:40:21 +0000
committerrsiddharth <s@ricketyspace.net>2017-06-24 21:40:21 +0000
commit20de4932b291ebd0a24e6fcf5152f972bd80d4e9 (patch)
tree595b69acf1ada046c36b31c03faa2c13c6a37f4f /net/ricketyspace
parent1ddaa7fa55a6cc8ef5515df4b540d830449a48df (diff)
Add net/ricketyspace/sicp/one/twentytwo.scm
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/one/twentytwo.scm48
1 files changed, 48 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/twentytwo.scm b/net/ricketyspace/sicp/one/twentytwo.scm
new file mode 100644
index 0000000..209afaf
--- /dev/null
+++ b/net/ricketyspace/sicp/one/twentytwo.scm
@@ -0,0 +1,48 @@
+;;;; Under Creative Commons Attribution-ShareAlike 4.0
+;;;; International. See
+;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+(define-module (net ricketyspace sicp one twentytwo)
+ #:use-module (net ricketyspace sicp utils)
+ #:export (timed-prime-test
+ search-for-primes))
+
+(define (square x)
+ (expt x 2))
+
+(define (divides? a b)
+ (= (remainder b a) 0))
+
+(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 (smallest-divisor n)
+ (find-divisor n 2))
+
+(define (prime? n)
+ (= n (smallest-divisor n)))
+
+(define (timed-prime-test n)
+ (newline)
+ (display n)
+ (start-prime-test n (runtime)))
+
+(define (start-prime-test n start-time)
+ (if (prime? n)
+ (report-prime (- (runtime) start-time))
+ #f))
+
+(define (report-prime elapsed-time)
+ (display " *** ")
+ (display elapsed-time) #t)
+
+(define (odd-and-prime n)
+ (and (odd? n) (timed-prime-test n)))
+
+(define (search-for-primes start end)
+ (cond ((> start end) '())
+ ((odd-and-prime start)
+ (cons start (search-for-primes (1+ start) end)))
+ (else (search-for-primes (1+ start) end))))