summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2017-09-02 02:28:37 +0000
committerrsiddharth <s@ricketyspace.net>2017-09-02 02:28:37 +0000
commit0d0aa694a5bc3465f5c9b780e7901c711c0e079c (patch)
tree843557736644a477384b3fba4e56d29bea331fb1 /net/ricketyspace
parent43093fc92209563110cb24d7968a9af9ad96632c (diff)
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 (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/one/twentyeight.scm30
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?)))
+
+