summaryrefslogtreecommitdiffstats
path: root/primes.scm
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2017-02-20 00:05:27 -0800
committerBryan Newbold <bnewbold@robocracy.org>2017-02-20 00:05:27 -0800
commitfa3f23105ddcf07c5900de47f19af43d1db1b597 (patch)
treeb2c6cce6b97698098f50cbc78c23fdc0f8d401ab /primes.scm
parentf24b9140d6f74804d5599ec225717d38ca443813 (diff)
downloadslib-fa3f23105ddcf07c5900de47f19af43d1db1b597.tar.gz
slib-fa3f23105ddcf07c5900de47f19af43d1db1b597.zip
Import Upstream version 2c3upstream/2c3
Diffstat (limited to 'primes.scm')
-rw-r--r--primes.scm28
1 files changed, 17 insertions, 11 deletions
diff --git a/primes.scm b/primes.scm
index 769e2bc..672e899 100644
--- a/primes.scm
+++ b/primes.scm
@@ -85,23 +85,26 @@
;; Is `n' Divisible By a Small Prime?
;;
-(define (primes:dbsp? n)
- (let ((limit (min (sqrt n) primes:max-small-prime))
- (divisible #f)
- )
- (do ((i 0 (1+ i)))
- ((let* ((divisor (vector-ref primes:small-primes i)))
- (set! divisible (= (modulo n divisor) 0))
- (or divisible (>= divisor limit)))
- divisible)
- )))
+(define primes:dbsp?
+ (let ((sqrt (cond ((provided? 'inexact) sqrt)
+ (else (require 'root) integer-sqrt))))
+ (lambda (n)
+ (let ((limit (min (sqrt n) primes:max-small-prime))
+ (divisible #f)
+ )
+ (do ((i 0 (1+ i)))
+ ((let* ((divisor (vector-ref primes:small-primes i)))
+ (set! divisible (= (modulo n divisor) 0))
+ (or divisible (> divisor limit)))
+ divisible)
+ )))))
;; Does `n' pass the R.-M. primality test for `m' random numbers?
;;
(define (primes:rm-prime? n m)
(do ((i 0 (1+ i))
- (x (+ 2 (random (- n 2)))))
+ (x (+ 2 (random (- n 2) primes:prngs))))
((or (= i m) (primes:rm-composite? n x))
(= i m))))
@@ -150,6 +153,9 @@
(set! y (modulo (* y z) modulus)))
))
+(define primes:prngs
+ (make-random-state "repeatable seed for primes"))
+
;; This table seems big enough so that making it larger really
;; doesn't have much effect.
;;