Project Euler NO28

原題鏈接

從數字1開始向右順時針方向移動,可以得到如下的5×5的螺旋:

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

可以算出對角線上數字之和是101.

1001×1001的螺旋中對角線上數字之和是多少?


public class Project28
{
	public static void main(String[] args)
	{
		long start = System.currentTimeMillis();
		System.out.print("answer:  ");

		howmany();

		long end = System.currentTimeMillis();
		System.out.print("time:  ");
		System.out.println(end - start);
	}

	static void howmany()
	{
		int sum = 1;
		int add = 0;
		int n = 0;
		for (int i = 1; i < 1001 * 1001; )
		{
			if (n % 4 == 0)
			{
				add += 2;
			}
			n++;
			i += add;
			sum += i;
		}

		System.out.println(sum);
	}
}

answer:  669171001
time:  0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章