zondag 27 februari 2011

Euler problem 28

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 the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

Solution 1: Using Linq
s = spiral number                     : 1  2  3  4  5 ... 501
r = ribbe length   (2*s-1)           : 1  3  5  7  9
d = numbers/spiral 4*(r-1)         : 0  8 16 24 32
e = numbers cummulative(e+=d) : 1  9 25 49 81 <= diag North-East
f = diag South-East (e-d+r-1)     : 1  3 13 31 57 <= e-6s+6

Sum = 2 * ( NE + SE) - 3



Solution 2:
Numbers on diagonal NE: n²      ( n is size of spiral 3, 5, 7, ... 1001 )
Numbers on diagonal SE: n² -3n + 3
Sum = 2 * (NE + SE) + 1


Problem28 =   669171001 elapsed time:    0 ms. Test Passed.




Geen opmerkingen:

Een reactie posten