home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d09xx
/
d0963.lha
/
SIOD
/
scm
/
prime.scm
< prev
next >
Wrap
Text File
|
1993-05-08
|
1KB
|
46 lines
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test)
(cond ((> (expt test 2) n) n)
((divides? test n) test)
(else (find-divisor n (add1 test)))))
(define (divides? x y)
(zero? (remainder y x)))
(define (prime? n)
(= n (smallest-divisor n)))
(define (expmod b e m)
(cond ((= e 0) 1)
((even? e)
(remainder (expt (expmod b (/ e 2) m) 2) m))
(else (remainder (* b (expmod b (- e 1) m)) m))))
(define (fermat-test n)
(define a (+ 2 (random (- n 2))))
(= (expmod a n n) a))
(define (carmicael-test n)
(do ((a 1 (1+ a)))
((<> (expmod a n n) a) a)))
(define (fast-prime? n times)
(cond ((= times 0) t)
((fermat-test n)
(fast-prime? n (sub1 times)))
(else nil)))
(define (jacobi a n)
(cond ((= a 1) 1)
((even? a) (* (jacobi (/ a 2) n) (expt -1 (/ (-1+ (expt n 2)) 8))))
(else (* (jacobi (remainder n a) a) (expt -1 (* (-1+ a) (-1+ n) 1/4))))))
(define (solovay-test n)
(define a (do ((a (+ 2 (random (- n 2))) (+ 2 (random (- n 2)))))
((= (gcd a n) 1) a)))
(= (expmod a (/ (-1+ n) 2) n) (jacobi a n)))
(define (fast-prime-sol? n times)
(do ((tim times (-1+ tim))
(good 0 (if (solovay-test n) (1+ good) good)))
((= tim 0) (< good (/ n 2)))))