# Sum of diagonals

1. Apr 21, 2005

### AntonVrba

Consider a spiral starting with the number 1 and moving to the right in a clockwise direction, below is an example of a 5 by 5 spiral and the diagonal is highlighted:

21 22 23 24 25
20 07 08 09 10
19 06 01 02 11
18 05 04 03 12
17 16 15 14 13

The sum of both diagonals is 101.

if the spiral formed in the same way to 1001 by 1001, then what is the sum of both diagonals?

2. Apr 23, 2005

### AntonVrba

HINT:

the number in the top right corner is n^2, the other corners are given by: n^2-n+1, n^2-2n+2, and n^2-3n+3.

3. Apr 23, 2005

### Galileo

Argh, my stupid computer crashed while I finished typing out the solution here.
Luckily my closed form formula was still copied to mthe memory:
Sum of diagonals for the nxn spiral (n is odd): (2/3)n^3+(1/2)n^2+(4/3)n-3/2

Which gives for n=1001: 669171001

4. Apr 23, 2005

### BicycleTree

I wrote a program and came up with the same answer.
Code (Text):

class Diagonals{
public static void main(String args[])
{
int x = 0, y = 0, sum = 0;
for(int i = 1; i <= 1001*1001; i++)
{
if(Math.abs(x) == Math.abs(y))
sum+=i;
if(y >= Math.abs(x))
x++;
else if((y <= -x && x > 0) || (y < x && x <= 0))
x--;
else if(x > 0)
y--;
else if(x < 0)
y++;
}
System.out.println(sum);
}
}

How can I prove my program equivalent to your formula?

5. Aug 9, 2011

### Andrex

You were once the only author on the net to publish this formula. How do you come to this result? I published a similar formula much later. Sorry for not quoting you.

My contribution in APL Quote Quad (December 20027) fell in the category "APL as a tool of thought".

Regards

6. Aug 11, 2011

### mathal

Tribute and addendum to Galileo's solution .
Perhaps someone can make use of the iteration math I am contributing to find a way to Galileo's formula.

Galileo's solution formula
(2/3)n^3+(1/2)n^2+(4/3)n-3/2

The algorithm I made up used n^2 +((n-1)^2)+1)*3 for each +2 iteration starting at 1.
Starting at 2 it yields the even diagonal sums.
Your formula for even diagonals would leave out the last -3/2. Yours is a straightforward answer. Good one Galileo.

mathal

Last edited: Aug 11, 2011
7. Aug 13, 2011

### Andrex

Erratum: I mean "December 2007" (not "December 20027").