According to the Project Euler website, “Project Euler is a series of challenging mathematical/computer programming problems…”

Always a fan of puzzles, I have decided to share my attempts and solutions here.

Note: If you are trying to solve Problem #28 for yourself, DO NOT READ AHEAD!


Problem #28

Solved 10/08/2009

Problem Description

This problem asks for the sum of the diagonals in a 1001x1001 number spiral. A sample 5x5 spiral looks like the one below, the totals of which are 101.

Solution

For this one, I drew a spiral number up to 81, or a 9x9 square. From there, I determined the patterns for the diagonals. I based my solution on even or odd numbers greater than 1. I used the odd numbers to calculate the upper-right diagonal, which are all perfect squares of the next odd number (3^2, 5^2, 7^2, etc). The even numbers were used to calculate the other three diagonals: upper-left -> n^2 + (n + 1); lower-left -> n^2 + 1; and lower-right -> n^2 - (n - 1). The total sum for these three diagonals is 3 _ n^2 + 3.

Code (PHP)

Final solution: 669171001