Primes and Polynomials

To put it mildly, the prime numbers are the subject of some mathematical interest, and of great mathematical mystery. Intriguingly, it’s possible to come up with a polynomial P:ZnZP : \mathbb{Z}^n \to \mathbb{Z} in nn variables such that the range of positive values taken on by PP (on nonnegative inputs) enumerates the primes. To put it more precisely, P(Nn)NP(\mathbb{N}^n) \cap \mathbb{N} is the set of prime numbers. This result is due to Yuri Matiyasevich.

The cleanest such construction is probably the Jones-Sato-Wada-Wiens polynomial, which has n=26n=26 variables and is of degree 2525. (This is convenient, because the English alphabet happens to have 2626 letters.) I won’t try to strain my poor website’s mathematical typesetting system by writing out the polynomial here, but you can see it in their original paper1. The main idea is to encode the relationship x=y!x = y! in a system of Diophantine equations, and then characterize the prime numbers using Wilson’s theorem. You can then combine the system of equations into one polynomial.

One day, this polynomial came up during lecture, and the professor jokingly said that we could come up with an assignment to the 2626 variables witnessing a small prime like 55 for “extra credit.” I thought it’d be a fun challenge, so I decided to try my hand at it. I ended up working out the numbers (or at least a closed-form expression for the numbers) for the prime 22, but anything higher would be completely computationally intractable, at least the way I did it. A more mathematically detailed description of the solution than what I could fit in a blog post is available as a PDF—this post is more of a quick play-by-play of the process that I went through.

Wisdom of the Ancients

(Relevant XKCD: #979)

After some online searching, the only thing that I could find was a Google Groups discussion from nearly twenty years ago, with one poster giving a purported assignment for the prime 22:

a = 20
b = 0
c = 0
d = 0
e = 3
f = 1
g = 0
h = 1
i = 0
j = 0
k = 0
l = 0
m = 1
n = 0
o = 244
p = 1
q = 1
r = 0
s = 0
t = 0
u = 1
v = 0
w = 0
x = 1
y = 0
z = 1

This very nearly works2, except that 16(k+1)3(k+2)(n+1)2+1f2=320.16(k+1)^3(k+2)(n+1)^2 + 1 - f^2 = 32 \neq 0. I figured that it couldn’t be too hard to fix, so I messed around with some numbers for a bit in Julia. But I found that the behavior of the polynomial was rather unpredictable, and with a search space consisting of 2626 parameters that I had no bound for, it was clear that I’d need a better approach.

Computers Are Our Friends

After skimming the original paper, it became apparent that there was a lot of structure to the way the authors had designed the polynomial. In particular, it all seemed to revolve around the Pell equation x2=(a21)y2+1.x^2 = (a^2-1)y^2 + 1. By reading the authors’ original proof, I was able to reverse engineer the values of a few of the smaller parameters, like kk (obviously) and nn. Given this foothold, I then needed to solve a chain of these Pell equations; I hashed out a quick Python program to search for a solution, confident that my work would be over soon.

Unfortunately, more than an hour later, my program still had not found a solution. Perhaps this would require some more refined intellectual firepower; I had ruled out pretty much any values small enough to find with brute force. I tried reading a textbook on solving Diophantine equations, but it just confused me, so I did the next best thing: I plugged the system into Mathematica and hoped it would work. To my surprise, Mathematica immediately identified a viable solution. To my dismay, it was far too large; while I could solve for some of the parameters, the remaining ones would be computationally out of reach.

So I did the first thing that all good mathematicians do: if you can’t solve a problem, try to solve an easier version. Rather than try the ambitiously large prime number 55, I decided to try to find a solution for the much more modest prime 22. This initially worked well; most of the numbers were pretty small, like f=17f = 17 and e=32e = 32. Unfortunately, I then ran into a bit of a problem:

FindInstance[e^3 (e + 2) (a + 1)^2 + 1 == o^2, {a, o}, Integers]

This returned:

a = 7901690358098896161685556879749949186326380713409290912
o = 8340353015645794683299462704812268882126086134656108363777

Clearly, things were getting a little out of hand. Perhaps Mathematica wasn’t returning the optimal solution? Unfortunately, the authors proved that this condition would force ae1+ee2=1427247692705959881058285969449495136382746655,a \ge e - 1 + e^{e-2} = 1427247692705959881058285969449495136382746655, so it seemed unlikely that a significantly smaller solution existed. I had gotten pretty far, and with some more careful reading of this paper and a related one3, I was able to get all of the parameters besides cc, dd, rr, and uu.

Numbers Are Hard

At this point, I was pretty sure that I was on the right track. By reverse engineering the original proofs and applying some computational power, I had arrived at a partial assignment satisfying almost every constraint I needed—except for two. Unfortunately, these remaining two constraints required a little bit of (fairly elementary) number theory to resolve, and I am pretty bad at that. So I did the second thing that all good mathematicians do: if you’re stumped, put the problem down for a month and do something else.

So I went and did some other stuff for a month. Later, after my first round of midterms, I decided to come back to this problem for a bit. I wasn’t expecting much, but to my surprise, everything seemed to make much more sense a month later. I still wasn’t able to figure out numeric values for the remaining parameters cc, dd, rr, and uu, but I did get closed form expressions that should (in theory) yield the correct answer. So yay, I guess?

If you’re interested, I’ll again plug the more mathematically detailed writeup, and I’ll mention a short program that verifies most of the constraints. There isn’t really a moral to this story; it was just kind of fun.


As a side note, I had originally titled this post “Prime Polynomials,” but I realized that this choice of title was unfortunate, because the term “prime polynomial” is also used to refer to irreducible polynomials over a unique factorization domain. This has little to do with the sense in which I was thinking of the term when I originally titled this post, and so I’ve changed the title to be less ambiguous.


  1. James P. Jones, Daihachiro Sato, Hidea Wada, and Douglas Wiens, Diophantine representation of the set of prime numbers, The American Mathematical Monthly, 83 (1976) 449-464.↩︎

  2. I later discovered his mistake: in the original paper, the authors start by listing the constraints with k+1k+1, with k1k \ge 1, as the prime, and then turn it into k+2k+2 in the final polynomial to allow k0k \ge 0; they mention this, but it’s easy to miss. It turns out that this off-by-one error in kk is rather difficult to resolve.↩︎

  3. Martin Davis, Hilbert’s tenth problem is unsolvable, The American Mathematical Monthly, 80 (1973) 233-269.↩︎