## Project Euler in F# – Problem 28

Sometimes, when I find a problem like Project Euler problem 28, I remember that human beings are smarter than computers.

Let's read the problem statement, it doesn't seem easy at first glance:

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 7 8 9 10

19 6 1 2 11

18 5 4 3 12

17 16 15 14 13

It can be verified that the sum of both diagonals is 101.

What is the sum of both diagonals in a 1001 by 1001 spiral formed in the same way?

If we don't think of anything *creative*, we are forced to write an algorithm to draw that 1001x1001 number spiral and then sum both diagonals together.

However, we can start noticing that the number in the top-right position is always *n*^2, where *n* is the length of one side of the spiral. In the example, the spiral is 5 by 5 and the top-right value is 25. In a 3 by 3 spiral it would be 9, 49 in a 7 by 7 spiral and so on.

If we concentrate on the outer ring of the spiral, then the inner sum can be obtained by recursion, so we only need to find a trick for the top-left, bottom-left and bottom-right numbers.

They are 21, 17 and 13 in a 5x5 spiral, so it seems like they can be computed starting from the top-right number and repeatedly subtracting 4 (i.e. *n - 1*).

Let's check this theory with the 3x3 spiral: the top-right number is 9, the other corners are 7, 5 and 3. We are indeed subtracting 2 (i.e. *n - 1*) each time!

Let's summarize: given a *n* x *n* spiral we have to sum *n^2*, *n^2 - (n-1)*, *n^2 - 2(n-1)* and *n^2 - 3(n-1)*.

Some elementary math and we get *4n^2 - 6n + 6* for each ring of the spiral.

The base case of the recursion is the 1x1 spiral and in that case the (degenerate) diagonal sum is 1.

Given these premises it is very easy to write the F# code to implement the algorithm:

let rec euler28 n = match n with | 1 -> 1 | _ -> euler28 (n-2) + (4 * n * n) - (6 * n) + 6

An improvement of the code above would be using *tail recursion*, which means that the last operation of the function should be a recursive call. This leads to a reduction of the stack space used from *O(n)* to *O(1)* and let us avoid stack overflow exceptions:

let tail_euler28 n = let rec spiral n sum = match n with | 1 -> sum + 1 | _ -> spiral (n - 2) (sum + (4 * n * n) - (6 * n) + 6) spiral n 0

As it is usually done in *tail recursion*, we define an *accumulator* parameter (*sum* in the example) which is used to store the partial result of the computation, eliminating the need to keep the state on the stack.

In both solutions I've left a bug that can lead to an infinite loop. Can you spot it?

Jorge EsquerMay 23rd, 2009 - 05:38

Would the bug be that, if you pass an even number as n, it’ll never match 1 because it will jump from 2 to 0 and thus keep subtracting 2 indefinitely?

Thedric WalkerMay 23rd, 2009 - 17:37

Pretty cool article. If I had to guess, I would say putting in any even number would cause this to blow up. This is because the exit condition assumes that n-2 will eventually result in 1. However, for even numbers that never happens.

But that’s just my guess.

Jorge EsquerMay 24th, 2009 - 00:30

Here’s my take with fold:

printf “%A” (Seq.fold (fun sum x -> sum + 4 * x * x – 6 * (x – 1)) 1 [3..2..1001])

claudioMay 25th, 2009 - 08:29

Jorge and Thedric, you both guessed it right!

The solution with fold is perfect while the recursive one is easier to understand and to use with academic purposes.

ggseJuly 15th, 2009 - 12:52

The sum is also given by a formula : for a n by n spiral, S = (4*n^3+3*n^2+8*n-9)/6.