diff options
Diffstat (limited to 'net/ricketyspace')
-rw-r--r-- | net/ricketyspace/sicp/one/twentyeight.scm | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/net/ricketyspace/sicp/one/twentyeight.scm b/net/ricketyspace/sicp/one/twentyeight.scm index bad63a0..ffc1e21 100644 --- 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?))) + + |