Sunday, 25 October 2009

#28

This problem can be solved by thought rather than brute force. Looking at the diagonals as 4 separate sums, the four diagonals form four number sequences which when summed give the value of the kth ring to be 4[(2k+1)^2-3k] (k>=1, k=0 is the first 1x1 grid).

Determining the link betweem the grid size and k was also trival, the ring size is 2k+1. Hence for a 1001x1001 grid, k = 500. We can simply sum the answer over t=1..k+1 to get the solution, and add one for the central 1.
let fk k = 4*((2*k+1)^2-3*k)
let s = 1 + (sum . map fk $ [1..500])

No comments:

Post a Comment