I noticed a simple pattern of odd squares traveling up the rightmost corner of the spiral and after a little bit of toying around with the numbers I was able to come up with my solution. There are some truly beautiful and well explained solutions in the forums, but I’m pretty happy with mine, I could have been a nice guy and split it up, but I had a rough time coming up with appropriate variable names. This is what you get.
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 both diagonals is 101.
What is the sum of both diagonals in a 1001 by 1001 spiral formed in the same way?
def sum_corners n return 1 if n == 1 4 * ((n * 2 - 1) ** 2 - (3 * n - 3)) + sum_corners(n - 1) end puts sum_corners((1001 + 1) / 2)