Vasu Balakrishnan’s Blog

Project Euler #28

leave a comment »


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

Answer: 669171001


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


Written by nbvasu

May 15, 2009 at 2:22 pm

Posted in C#, Euler, General

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: