Miller-Rabin primality test (Clojure)

From LiteratePrograms
Jump to: navigation, search
Other implementations: C | C, GMP | Clojure | Java

The Miller-Rabin primality test is a simple probabilistic algorithm for determining whether a number is prime or composite that is easy to implement. It proves compositeness of a number using the following formulas:

Suppose 0 < a < n is coprime to n (this is easy to test using the GCD). Write the number n−1 as 2^s \cdot d, where d is odd. Then, provided that all of the following formulas hold, n is composite:


a^{d} \not\equiv 1\pmod{n}

a^{2^rd} \not\equiv -1\pmod{n} for all 0 \le r \le s-1

If a is chosen uniformly at random and n is prime, these formulas hold with probability 1/4. Thus, repeating the test for k random choices of a gives a probability of 1 − 1 / 4k that the number is prime. Moreover, Jaeschke showed that any 32-bit number can be deterministically tested for primality by trying only a=2, 7, and 61.:Other implementations: C | C, GMP | Clojure | Java [1]

Download code

[edit] References

Other implementations: C | C, GMP | Clojure | Java
  1. Jaeschke, Gerhard (1993), "On strong pseudoprimes to several bases", Mathematics of Computation 61 (204): 915–926, doi:10.2307/2153262.
Download code


(defn factorize-out [n x]
  (loop [acc n exp 0]
    (if (zero? (rem acc x))
      (recur (/ acc x) (inc exp))
      (hash-map :exponent exp :rest acc))))

(use '[clojure.contrib.math :only (expt)])
(defn expt-rem [n e m]
  (loop [r 1, b n, e e]
                (if (zero? e) r
                        (recur
                                (if (odd? e) (rem (* r b) m) r)
                                (rem (expt b 2) m)
                                (bit-shift-right e 1)))))

(defn miller-rabin? [accuracy num]
        (cond
                (even? num) (= num 2)
                (< num 2) 'false?
                :else
                (let
                        [m (factorize-out (dec num) 2)
                        d (:rest m)
                        s (:exponent m)
                        witness? (fn [r x]
                                (cond
                                        (or (= x 1)(> r (dec s))) 'false
                                        (= x (dec num)) 'true
                                        :else (recur (inc r)(rem (* x x) num))))
                        investigate? (fn [k]
                                (if (> k accuracy) 'true
                                        (let
                                                [a (+ 2 (rand-int (- num 4)))
                                                x (expt-rem a d num)]
                                                (if (or (= x 1)(= x (dec num))(witness? 1 (expt-rem x 2 num)))
                                                        (recur (inc k))
                                                        'false))))]
                        (investigate? 1))))

(def *prime-accuracy* 10)
(def isPrime? (partial miller-rabin? *prime-accuracy*))
Download code
hijacker
hijacker
hijacker
hijacker