sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit f800c009ac81eaef87dced7ec9e35f8444a3fb7a
parent b51382f42a102146ffdf42497fd9fd2cbf914980
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 19 Aug 2017 02:46:54 +0000

net: Add carmichael-numbers-fool-fermat-test?

* net/ricketyspace/sicp/one/twentyseven.scm
(fermat-test): Update function.
(fast-prime?): Remove function.
(prime?): New function.
(carmichael-numers-fool-fermat-test?): Remove function.
(carmichael-numers-fool-fermat-test?): New function.

Diffstat:
net/ricketyspace/sicp/one/twentyseven.scm | 20++++++++------------
1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/net/ricketyspace/sicp/one/twentyseven.scm b/net/ricketyspace/sicp/one/twentyseven.scm @@ -4,7 +4,7 @@ (define-module (net ricketyspace sicp one twentyseven) #:use-module (srfi srfi-1) - #:export (carmichael-numers-fool-fermat-test?)) + #:export (carmichael-numbers-fool-fermat-test?)) (define carmichael-numbers '(561 1105 1729 2465 2821 6601)) @@ -20,18 +20,14 @@ m)))) (define (fermat-test n) - (define (try-it a) + (define (pass? a) (= (expmod a n n) a)) - (try-it (+ 1 (random (- n 1))))) + (fold (lambda (a p) (and (pass? a) p)) #t (iota (1- n) 1))) -(define (fast-prime? n times) - (cond ((= times 0) #t) - ((fermat-test n) (fast-prime? n (- times 1))) - (else #f))) +(define (prime? n) + (if (fermat-test n) #t #f)) -(define (carmichael-numers-fool-fermat-test?) +(define (carmichael-numbers-fool-fermat-test?) "Returns #t if all `carmichael-numbers` pass (fool) the fermat test." - (let* ((fooled-test? (lambda (n) (fast-prime? n 100000))) - (fooled? (lambda (result) (eq? result #t))) - (results (map fooled-test? carmichael-numbers))) - (every fooled? results))) + (define (fooled-test? n) (prime? n)) + (fold (lambda (n p) (and (fooled-test? n) p)) #t carmichael-numbers))