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 1001×1001 number spiral. A sample 5×5 spiral looks like the one below, the totals of which are 101.
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
Solution
For this one, I drew a spiral number up to 81, or a 9×9 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 (lang)
Final solution: 669171001
There are no comments yet.