Manually applied Jens Axel's patch to add `random-prime'

This commit is contained in:
Neil Toronto 2012-11-17 21:19:46 -09:00
parent e655e97a83
commit 68af24780d
2 changed files with 21 additions and 0 deletions

View File

@ -12,6 +12,7 @@
; primes
nth-prime
random-prime
next-prime untyped-next-prime
next-primes
prev-prime untyped-prev-prime
@ -286,6 +287,14 @@
(for/fold: ([p : Prime 2]) ([m (in-range n)])
(next-prime p))]))
(: random-prime : Z -> Prime)
(define (random-prime n)
(when (<= n 2)
(raise-argument-error 'random-prime "Natural > 2" n))
(define p (random-natural n))
(if (prime? p)
p
(random-prime n)))
;;;
;;; FACTORIZATION

View File

@ -304,6 +304,18 @@ Returns the @racket[n]th positive prime; @racket[n] must be nonnegative.
(nth-prime 2)]
}
@defproc[(random-prime [n Integer]) Natural]{
Returns a random prime smaller than @racket[n], which must be greater than @racket[2].
The function @racket[random-prime] picks random numbers
below @racket[n] until a prime is found.
@interaction[#:eval untyped-eval
(random-prime 10)
(random-prime 10)
(random-prime 10)]
}
@defproc[(next-prime [z Integer]) Integer]{
Returns the first prime larger than @racket[z].