Project Euler Problem 028

http://projecteuler.net/index.php?section=problems&id=28
http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2028

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?

対角線上の数字の合計を求める。


辺の長さから角の数を求めて合計する関数を作って、順々に加算していった。

import datetime


def sum_of_four_corners(n):
    
    result = 0
    
    v = (n - 2) ** 2 + n - 1
    for i in xrange(0, 4):
        result += v + (i * (n - 1))
        
    return result


def euler028():
    
    result = 1
    for i in xrange(3, 1002, 2):
        result += sum_of_four_corners(i)
    
    print result    
    

begin = datetime.datetime.now()
euler028()
end   = datetime.datetime.now()
print end - begin

答え: 669171001
実行時間: 0.001385秒くらい