Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215607
  • 博文数量: 68
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-08 09:53
文章分类
文章存档

2012年(29)

2011年(3)

2010年(18)

2009年(18)

我的朋友

分类: C/C++

2012-01-30 21:46:47

问题:

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?


答案: 669171001

#include
/*
 * i = 1 , 1
 * i = 3 , 3 5 7 9 q=2
 * i = 5 , 13 17 21 25 q=4
 *  .
 *  .
 *  .
 * i = n , n^2-3*(n-1) n^2-2*(n-1) n^2-(n-1) n^2 q=n-1
 * n = 2k*1, k in [1...500]
 * sum = (16*k^2+4*k+4)_(k 1...500)+1
 *     = 16*kmax*(kmax+1)*(2*kmax+1)/6+4*kmax*(kmax+1)/2+4*kmax+1
 */

int main(void)
{
    int i = 500;
    long sum = 0L;

    sum = 8*i*(i+1)*(2*i+1)/3 + 2*i*(i+1) + 4*i +1;
    printf("%li\n", sum);
    return 0;
}


阅读(823) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~