Blog

Mathematical trolling: the Willans prime formula

It has long been of interest to study any regularity in the prime numbers, a question which can be phrased as: “Is there a formula for the nth prime number?” While there has been a fair bit of progress and understanding (e.g. the work of Riemann, the prime-number theorem), in 1964 someone named(?) C. P. Willans from the University of Birmingham gave the following “troll” answer, answering the question literally while flagrantly violating its spirit.

pn=1+i=12n(nj=1i(cos(j1)!+1jπ)2)1/n

This ridiculous expression, which we’ll understand better below, is indeed a “formula” for the nth prime number, but it is in nearly every way worse than just saying “the nth prime number”. Two articles from a couple of decades later have some words on such formulas:

Nevertheless, the formula is surprising at first glance and can be interesting to understand: it is mentioned on Wikipedia and there’s an excellent YouTube video about it.

It is based on a bunch of tricks, and the video does a good job of explaining them from the bottom up, so as a complement, let’s do it top-down.

The nth prime in terms of the prime-counting function

Let π(m) denote the number of primes not greater than m, e.g. π(10)=4 because there are four primes 2,3,5,7 not greater than 10, and π(11)=5 because 11 is the fifth prime. (For more, see prime-counting function.)

Then, we can write an expression (circular definition) for pn as: pn=the smallest number m such that π(m)n

With the additional knowledge that pn is at most 2n (by Bertrand’s postulate), we can write this as: pn=1+i=12n[π(i)<n] because if the nth prime is m, then each i<m will add 1 to the sum, while m and higher will add 0.

Next, we can write [cn]=(nc)1/nbecause for c>n we have n/c<1 so (n/c)1/n<1, while for cn we have nn/c1 so n1/n(n/c)1/n1 (and the global maximum of n1/n is e1/e1.445<2).

Putting these together (and π(i)<n being the same as 1+π(i)n), we can write: pn=1+i=12n(n1+π(i))1/n This is already a “formula” for pn, if we can express the π(i) part as a “formula”.

The prime-counting function

1+π(i)=j=1i[j is prime or 1]=j=1i[(j1)!+1j is an integer ] by Wilson’s theorem, which states that j divides (j1)!+1 iff j is either prime or 1.

We can represent “is an integer” with this trick: [x is an integer]=(cos(xπ))2 as cos(xπ) is 1 or 1 when x is an integer, and strictly less than 1 in absolute value otherwise.

And with that, we’re done!

Summary

Thus, Willans’s formula is “merely” a clever putting together of these tricks:

In programming terms, it is like starting with the following naive and inefficient function for returning the nth prime:

def nth_prime(n):
    num_primes_seen = 0
    for i in range(1, 2**n):
        # TODO: implement is_prime, returning 0 or 1
        num_primes_seen += is_prime(i)
        if num_primes_seen >= n:
            return i

and making it less efficient, by recomputing num_primes_seen (the number of primes i) from scratch in a nested loop, and by choosing an awfully slow implementation of is_prime(i):

def nth_prime(n):
    for i in range(1, 2**n):
        # count = 1 + pi(n) = 1 + (number of primes <= i)
        count = sum((math.factorial(j - 1) + 1) % j == 0 
                    for j in range(1, i + 1))
        if count > n:
            return i

and making it even worse by always running the loop up to 2n. This gives the following very inefficient program:

def nth_prime(n):
    p = 1
    for i in range(1, 2**n):
        count = sum((math.factorial(j - 1) + 1) % j == 0
                    for j in range(1, i + 1))
        p += (count <= n)
    return p

This (absurdly inefficient) program is now in a form amenable to “encoding” using formula tricks, and that is precisely what the Willans formula does.