diff options
author | rsiddharth <s@ricketyspace.net> | 2017-07-22 17:31:33 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2017-07-22 17:31:33 +0000 |
commit | 52afc19f43872517e443a948cc19abbad1afdbdf (patch) | |
tree | 7aabaf4d81b56b5b534a25cfd2a75af3dab40a40 | |
parent | 30535ec1f959d828241b0f9863709587094ddeec (diff) |
net: Add (net ricketyspace sicp one twentyfour).
* net/ricketyspace/sicp/one/twentyfour.scm: New module.
-rw-r--r-- | net/ricketyspace/sicp/one/twentyfour.scm | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/twentyfour.scm b/net/ricketyspace/sicp/one/twentyfour.scm new file mode 100644 index 0000000..58e4fcf --- /dev/null +++ b/net/ricketyspace/sicp/one/twentyfour.scm @@ -0,0 +1,63 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (net ricketyspace sicp one twentyfour) + #:use-module (srfi srfi-1) + #:export (three-primes-gt-f + three-primes-gt-1000-f + three-primes-gt-10000-f + three-primes-gt-100000-f + three-primes-gt-1000000-f)) + +(define (square x) + (* x x)) + +(define (expmod base exp m) + (cond ((= exp 0) 1) + ((even? exp) + (remainder (square (expmod base (/ exp 2) m)) + m)) + (else + (remainder (* base (expmod base (- exp 1) m)) + m)))) + +(define (fermat-test n) + (define (try-it a) + (= (expmod a n n) a)) + (try-it (+ 1 (random (- n 1))))) + +(define (fast-prime? n times) + (cond ((= times 0) #t) + ((fermat-test n) (fast-prime? n (- times 1))) + (else #f))) + +(define (odd-and-prime n) + (and (odd? n) (fast-prime? n (quotient n 2)))) + +(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)))) + +(define (three-primes-gt-f n) + (three-primes-gt-iter n 10 '())) + +(define (three-primes-gt-iter n e l) + (cond ((> (length l) 3) (take l 3)) + (else (three-primes-gt-iter n + (+ e 10) + (search-for-primes n e))))) + +(define (three-primes-gt-1000-f) + (three-primes-gt 1000)) + +(define (three-primes-gt-10000-f) + (three-primes-gt 10000)) + +(define (three-primes-gt-100000-f) + (three-primes-gt 100000)) + +(define (three-primes-gt-1000000-f) + (three-primes-gt 1000000)) |