# A magic square filled with consecutive primes

In 1988 Martin Gardner offered a \$100 prize for the first person to produce a magic square filled with consecutive primes. Later that year, Harry Nelson found 22 solutions using a Cray computer.

 1,480,028,201 1,480,028,129 1,480,028,183 1,480,028,153 1,480,028,171 1,480,028,189 1,480,028,159 1,480,028,213 1,480,028,141

Gardner said that the square above is “almost certainly the one with the lowest constant possible for such a square.”

It’s easy to verify that the numbers above are consecutive primes. Here’s a little Python code for the job. The function `nextprime(x, i)` gives the next `i` primes after `x`. We call the function with `x` equal to one less than the smallest entry in the square and it prints out all the entries.

```    from sympy import nextprime
for i in range(1,10):
print( nextprime(148028128, i) )
```

If you’re looking for more than a challenge, verify whether Gardner’s assertion was correct that the square above uses the smallest possible set of consecutive primes.

By the way, assuming Harry Nelson’s Cray was the Y-MP model that came out in 1988, here are its specs according to Wikipedia:

The Y-MP could be equipped with two, four or eight vector processors, with two functional units each and a clock cycle time of 6 ns (167 MHz). Peak performance was thus 333 megaflops per processor. Main memory comprised 128, 256 or 512 MB of SRAM.

Related: Magic squares from a knight’s tour and a king’s tour.

For daily tweets on algebra and other math, follow @AlgebraFact on Twitter.

## 5 thoughts on “A magic square filled with consecutive primes”

1. Duncan Reynolds

Very cool! There’s a typo in your Python – should be 1480028128 not 1480082128 :-)

2. Thanks!

3. Hugh Myers

You just pick on me with your blogs because you know I’m a sucker for primes :( Great (if small) blog—kudos. I’d forgotten the Gardener article, thanks for the memory. BTW, did you notice that the latest Cray is upgrading to the latest INTEL chip. I’m sure that this is some kind of irony, but I’m not sure just what and how…

4. If you normalize a magic square to make the lowest number 0, you’re left with 8 unique values.

It should not be too hard to calculate all solutions of a reasonable size, then match rolling sets of consecutive primes against them.

5. nextprime(x,i) doesn’t give the next i primes after x but the i-th prime following x. But it’s quite inefficient to re-compute each time all these primes. It would be more efficient to start with p=(first prime) and then do p=nextprime(p+1) until the last one is reached. @Lucas: yes indeed, there are not many different possible configurations for a magic square based on {x1 < x2 <= … <= x9 }, and there are criteria allowing to skip a check for many of those; and today it's a matter of milliseconds to compute the first 10^7 primes, which is already beyond those used here (primepi(148028129) = 8339708). See also oeis.org which has many entries about magic squares made of (consecutive or not) primes.