44 lines
1.0 KiB
Plaintext
44 lines
1.0 KiB
Plaintext
(defn square [n]
|
|
(* n n))
|
|
|
|
(defn divides? [a b]
|
|
(= (mod b a) 0))
|
|
|
|
(defn smallest-divisor [n]
|
|
(defn iter [n test]
|
|
(cond
|
|
(> (square test) n) n
|
|
(divides? test n) test
|
|
(iter n (+ test 1))))
|
|
(iter n 2))
|
|
|
|
(defn prime? [n]
|
|
(= n (smallest-divisor n)))
|
|
|
|
(defn search-for-primes [start count]
|
|
(defn report-time [n elapsed-time]
|
|
(printf "%d *** %fus" n (* elapsed-time 1000000.0)))
|
|
(defn start-prime-test [n start-time]
|
|
(if (prime? n)
|
|
(do
|
|
(report-time n (- (os/clock :cputime) start-time))
|
|
true)
|
|
false))
|
|
(defn times-prime-test [n]
|
|
(start-prime-test n (os/clock :cputime)))
|
|
(defn iter [test n]
|
|
(cond
|
|
(= n 0) 0
|
|
(times-prime-test test) (iter (+ test 2) (- n 1))
|
|
(iter (+ test 2) n)))
|
|
(cond
|
|
(= 0 (mod start 2)) (iter (+ start 1) count)
|
|
(iter start count)))
|
|
|
|
(search-for-primes 1000 3)
|
|
(search-for-primes 10000 3)
|
|
(search-for-primes 100000 3)
|
|
(search-for-primes 1000000 3)
|
|
(search-for-primes 10000000 3)
|
|
(search-for-primes 100000000 3)
|