Euler Problem 28: The Ulam Spiral and Quadratic primes

Euler Problem 28 takes us to the world of the Ulam Spiral. This is a spiral that contains sequential positive integers in a square spiral, marking the prime numbers. Stanislaw Ulam discovered that a lot of primes are located along the diagonals. These diagonals can be described as polynomials. The Ulam Spiral is thus a way of generating quadratic primes (Euler Problem 27).

Euler Problem 28 Definition

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20 07 08 09 10
19 06 01 02 11
18 05 04 03 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

Proposed Solution

To solve this problem we do not need to create a matrix. This code calculates the values of the corners of a matrix with size $n$. The lowest number in the matrix with size $n$ is $n(n-3)+4$. The numbers increase by $n-1$. The code steps through all matrices from size 3 to 1001. The solution uses only the uneven sized matrices because these have a centre. The answer to the problem is the sum of all numbers.

```size &lt;- 1001 # Size of matrix
answer &lt;- 1 # Starting number

## Define corners of subsequent matrices
for (n in seq(from = 3, to = size, by = 2)) {
corners &lt;- seq(from = n * (n - 3) + 3, by = n - 1, length.out = 4)
}
```

Plotting the Ulam Spiral

We can go beyond Euler Problem 28 and play with the mathematics. This code snippet plots all the prime numbers in the Ulam Spiral. Watch the Numberphile video for an explanation of the patterns that appear along the diagonals.

The code creates a matrix of the required size and fills it with the Ulam Spiral. The code then identifies all primes using the is.prime function from Euler Problem 7, as visualised on the top of this article. You can download  the latest version of this code from GitHub.

```size &lt;- 201 # Size of matrix
ulam &lt;- matrix(ncol = size, nrow = size)
mid &lt;- floor(size / 2 + 1)
ulam[mid, mid] &lt;- 1
for (n in seq(from = 3, to = size, by = 2)) {
numbers &lt;- (n * (n - 4) + 5) : ((n + 2) * ((n + 2) - 4) + 4)
d &lt;- mid - floor(n / 2)
l &lt;- length(numbers)
ulam[d, d:(d + n - 1)] &lt;- numbers[(l - n + 1):l]
ulam[d + n - 1, (d + n - 1):d] &lt;- numbers[(n - 1):(n - 2 + n)]
ulam[(d + 1):(d + n - 2), d] &lt;- numbers[(l - n):(l - 2 * n + 3)]
ulam[(d + 1):(d + n - 2), d + n - 1] &lt;- numbers[1:(n - 2)]
}
ulam.primes &lt;- apply(ulam, c(1, 2), is.prime)

library(ggplot2)
library(reshape2)
ulam.primes &lt;- melt(ulam.primes)
ggplot(ulam.primes, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
scale_fill_manual(values = c(&quot;white&quot;, &quot;black&quot;)) +
guides(fill =FALSE) +
theme(void)
```

8 thoughts on “Euler Problem 28: The Ulam Spiral and Quadratic primes”

1. Something wrong in the code – first you create a matrix:
ulam <- matrix(ncol = size, nrow = size)

next you correctly set the middle number:
ulam[mid, mid] <- 1

but later, you reference it as a variable:
ulam <- numbers[(l – n + 1):l]

It can't work this way!

1. Hi World Observer,

Thanks for pointing this out. The SyntaxHighlighter plugin removed some of the code because it was confused by “[c”. Now that I have changed the variable c to d, the code displays correctly.

I have updated the code in the post.

Peter

2. Actually, you should replace:

ulam <- numbers[(l – n + 1):l]
ulam <- numbers[(n – 1):(n – 2 + n)]

with:

ulam[c, c:(c + n -1)] <- numbers[(l – n + 1):l]
ulam[c + n -1, (c + n -1):c] <- numbers[(n – 1):(n – 2 + n)]

1. Indeed, see my response to your previous comment. Thanks again for helping out.

1. Great!

Thanks for interesting article.

This site uses Akismet to reduce spam. Learn how your comment data is processed.