From 20de4932b291ebd0a24e6fcf5152f972bd80d4e9 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 24 Jun 2017 21:40:21 +0000 Subject: Add net/ricketyspace/sicp/one/twentytwo.scm --- net/ricketyspace/sicp/one/twentytwo.scm | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 net/ricketyspace/sicp/one/twentytwo.scm 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 +;;;; . + +(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)))) -- cgit v1.2.3