sicp

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

commit 0d0aa694a5bc3465f5c9b780e7901c711c0e079c
parent 43093fc92209563110cb24d7968a9af9ad96632c
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat,  2 Sep 2017 02:28:37 +0000

net: Update (net ricketyspace sicp one twentyeight).

* net/ricketyspace/sicp/one/twentyeight.scm
(carmichael-numbers-pass?, prime-numbers-pass?)
(even-numbers-pass?, run-tests): New functions.

Diffstat:
net/ricketyspace/sicp/one/twentyeight.scm | 30+++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/net/ricketyspace/sicp/one/twentyeight.scm b/net/ricketyspace/sicp/one/twentyeight.scm @@ -4,7 +4,7 @@ (define-module (net ricketyspace sicp one twentyeight) #:use-module (srfi srfi-1) - #:export (miller-rabin-test prime?)) + #:export (miller-rabin-test prime? run-tests)) (define (sqmod x m) "Return x^2 if `x^2 mod m` is not equal to `1 mod m` @@ -32,3 +32,31 @@ and x != m - 1 and x != 1; 0 otherwise." (define (prime? n) (if (miller-rabin-test n) #t #f)) + + +;;; Tests + +(define (carmichael-numbers-pass?) + "Return #t if the sample carmichael numbers are detected as non-prime." + (let ((numbers '(561 1105 1729 2465 2821 6601))) + (cons "carmichael-numbers-pass?" + (fold (lambda (n p) (and (not (prime? n)) p)) #t numbers)))) + +(define (prime-numbers-pass?) + "Return #t if the sample prime numbers are detected as prime" + (let ((numbers '(311 641 829 599 809 127 419 13 431 883))) + (cons "prime-numbers-pass?" + (fold (lambda (n p) (and (prime? n) p)) #t numbers)))) + +(define (even-numbers-pass?) + "Return #t if the sample even numbers are detected non-prime" + (let ((numbers '(302 640 828 594 804 128 414 12 436 888))) + (cons "prime-numbers-pass?" + (fold (lambda (n p) (and (not (prime? n)) p)) #t numbers)))) + +(define (run-tests) + (map (lambda (test) (test)) (list carmichael-numbers-pass? + prime-numbers-pass? + even-numbers-pass?))) + +