

 This page uses content from Wikipedia. The original article was at Miller–Rabin primality test. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



The Miller–Rabin primality test or Rabin–Miller primality test is a primality test: an algorithm which determines whether a given number is prime or not.

The algorithm, as modified by Michael O. Rabin to avoid the generalized Riemann hypothesis, is a probabilistic algorithm.

The pseudocode, from Wikipedia is:

Input: n > 2, an odd integer to be tested for primality;
       k, a parameter that determines the accuracy of the test
Output: composite if n is composite, otherwise probably prime
write n − 1 as 2s·d with d odd by factoring powers of 2 from n − 1
LOOP: repeat k times:
   pick a randomly in the range [2, n − 1]
   x ← ad mod n
   if x = 1 or x = n − 1 then do next LOOP
   repeat s − 1 times:
      x ← x2 mod n
      if x = 1 then return composite
      if x = n − 1 then do next LOOP
   return composite
return probably prime

 The nature of the test involves big numbers, so the use of "big numbers" libraries (or similar features of the language of your choice) are suggested, but not mandatory.
 Deterministic variants of the test exist and can be implemented as extra (not mandatory to complete the task)


with javascript_semantics
function powermod(atom a, atom n, atom m)
-- calculate a^n%mod
    atom p = a, res = 1
    while n do
        if and_bits(n,1) then
            res = mod(res*p,m)
        end if
        p = mod(p*p,m)
        n = floor(n/2)
    end while
    return res
end function
 
function witness(atom n, atom s, atom d, sequence a)
-- n-1 = 2^s * d with d odd by factoring powers of 2 from n-1
    for i=1 to length(a) do
        atom x = powermod(a[i], d, n), y, w=s
        while w do
            y = mod(x*x,n)
            if y == 1 and x != 1 and x != n-1 then
                return false
            end if
            x = y
            w -= 1
        end while
        if y != 1 then
            return false
        end if
    end for
    return true;
end function
 
function is_prime_mr(atom n)
    if (mod(n,2)==0 and n!=2) 
    or (n<2) 
    or (mod(n,3)==0 and n!=3) then
        return false
    elsif n<=3 then
        return true
    end if
    atom d = floor(n/2)
    atom s = 1;
    while and_bits(d,1)=0 do
        d /= 2
        s += 1
    end while
 
    sequence a
    if n < 1373653 then
        a = {2, 3}
    elsif n < 9080191 then
        a = {31, 73}
    elsif (machine_bits()=32 and n < 94910107)
       or (machine_bits()=64 and n < 4295041217) then
        a = {2, 7, 61}
    else
        puts(1,"limits exceeded\n")
        return 0
    end if
    return witness(n, s, d, a)
end function
 
sequence tests = {999983,999809,999727,52633,60787,999999,999995,999991}
for i=1 to length(tests) do
    printf(1,"%d is %s\n",{tests[i],{"composite","prime"}[is_prime_mr(tests[i])+1]})
end for
