diff options
author | rsiddharth <s@ricketyspace.net> | 2017-08-19 02:46:54 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2017-08-19 02:46:54 +0000 |
commit | f800c009ac81eaef87dced7ec9e35f8444a3fb7a (patch) | |
tree | f5518d0bbdd69bbf53d4cde0f6acd2e701ed0b7e | |
parent | b51382f42a102146ffdf42497fd9fd2cbf914980 (diff) |
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.
-rw-r--r-- | net/ricketyspace/sicp/one/twentyseven.scm | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/net/ricketyspace/sicp/one/twentyseven.scm b/net/ricketyspace/sicp/one/twentyseven.scm index 891432f..cc50dc9 100644 --- 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)) |