## Primality Checking

### May 1, 2009

In a previous exercise you wrote a function that returned a list of prime numbers, and in another exercise you used that function to find a particular prime number. This exercise looks at prime numbers from a different perspective by considering a function that takes a number and determines if it is prime.

The algorithm that we will consider was developed initially by Gary Miller and refined by Michael Rabin, and is probabilistic in nature. It works like this: Express the odd number *n* to be factored as n = 2^{r} s + 1 with *s* odd. Then choose a random integer *a* with 1 ≤ *a* ≤ *n*-1 and check if *a ^{s}* ≡ 1 (mod

*n*) or

*a*

^{2j s}≡ -1 (mod

*n*) for some 0 ≤

*j*≤

*r*-1. (Some browsers render that last equation poorly; it’s

*a*raised to the power 2 to the

*j*times

*s*.) A prime number will pass the check for all

*a*. A composite number will pass the check for about 1/4 of the possible

*a*s and fail the check for the remaining 3/4 of the possible

*a*s. Thus, to determine if a number

*n*is prime, check multiple

*a*s; if

*k*

*a*s are checked, this algorithm will err less than one time in 4

^{k}. Most primality checkers set

*k*to somewhere between 25 and 50, making the chance of error very small.

Your task is to write a function that determines if an input number *n* is prime, then to determine if 2^{89}-1 is prime. When you are finished, you are welcome to read or run a suggested solution, or post your solution or discuss the exercise in the comments below.

Pages: 1 2

[...] Praxis – Primality Checking By Remco Niemeijer Today’s Programming Praxis problem is about checking whether or not a number is prime. We’re supposed [...]

My Haskell solution (see http://bonsaicode.wordpress.com/2009/05/01/programming-praxis-primality-checking/ for a version with comments):

I’m slowly working my way through old exercises now that I have some free time. Here’s

my attempt in Common Lisp; I’m trying to learn the language, even though I already had Pythoned Miller-Rabin previously.

Wow, so much code needed in OCaml to solve this exercise in a self-contained fashion! First of all, a general-purpose function to return a random

`Big_int`

between 0 and a specified`max`

(exclusive):(You can’t go much lower level than this.) Next, some utility functions on

`Big_int`

s:Finally, an imperative-style Rabin-Miller test:

Two optimizations were applied: first, the random a should be greater than 1; second, the successive powers of a in the inner loop are calculated inductively by (modular) squaring.

I made a mistake in

`random_big_int`

. Lines 10 and 11 are wrong, they should read:This is a Haskell program I wrote (ironically enough around May 2009, though I hadn’t heard about this website back then.) This was my first relatively non-trivial Haskell program.

To test 2^89 -1 use,

.\miller-rabin.exe -t 618970019642690137449562111

Rewrote the Haskell Miller-Rabin in Clojure as my first Clojure program. It is line-by-line translation, pretty much. Main difference is that one can’t generate random numbers > 2^63, so that is taken into account when generating tests.