## Project Euler #28

*Problem*: What is the sum of both diagonals in a 1001 by 1001 spiral?

Answer: **669171001**

*Solution*:

If you look at the right diagonal numbers they form a pattern. Each number is (2n + 1) ^ 2 where n is the level. At each level you need 4 numbers, the difference between each number is 2*n. To generate 1001 spiral, you need to go 500 levels. Once I figured this out, the implementation was straight forward.

var lOutput = from lLevel in FunExtensions.GenerateNumbers(1, 500, pInt => pInt) from lNum in FunExtensions.GenerateNumbers(0, 3, pInt => pInt) select Math.Pow(2*lLevel + 1, 2) - (2*lLevel*lNum); var lAnswer = lOutput.Sum() + 1;

*Time*: 17 ms

Advertisements

## Leave a Reply