eBash

It is not the mountain we conquer but ourselves

Project Euler Problem 28

| Comments

Table of Contents

1 Problem

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?

2 Solution

观察数字1,3,5,7,9,13,17,21,25,易知对角线数字递增规律为连续4个差2(3 + 2 = 5),连续4个差4(9 + 4 =13)…连续4个差(n-1);

当然我们也可以从中得出递推关系,观察右上对角线数字,1, 9, 25为1, 3, 5的平方,所有我们很容易知道f(n)的最右上的数字为 \(n^2\),则f(n)为f(n-2)加上最外围四个数字:
f(n) = f(n - 2) + 4 \(n^2\) - 6(n - 1);
一个简单的等差数列,最后求得:
\begin{eqnarray*} f(n) = \frac{2}{3}(n - 1)^{3} + \frac{5}{2}(n - 1)^2 + \frac{13}{3}(n - 1) + 1 \end{eqnarray*}

3 Answer

669171001

Source:C++

Comments