Created
October 28, 2011 08:59
-
-
Save Crazy-Owl/1321909 to your computer and use it in GitHub Desktop.
Random prime numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun random-number (length &key (current 0) odd) | |
(cond | |
((and odd (<= length 1)) (if (= 1 (mod current 2)) current (+ 1 current))) | |
((<= length 1) current) | |
(t (random-number (- length 1) :current (+ (ash current 1) (random 2)) :odd odd)))) | |
(defun check-prime (n rounds) | |
(let | |
((basic-primes '(2 3 5 7 9 11 13 17 19 23 29 31 37 41))) | |
(if (loop for i in basic-primes | |
do (when (= 0 (mod n i)) (return t)) | |
finally (return nil)) | |
nil | |
(loop for i from 1 to rounds do | |
(if (= 0 (mod n (random-number rounds))) | |
(return nil)) | |
finally (return t))))) | |
(defun random-prime (length &optional (current nil current-p)) | |
(cond | |
((and current-p (check-prime current (- length 1))) current) | |
(current-p (random-prime length (+ 2 current))) | |
(t (random-prime length (random-number length :odd t))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment