Problem 28
Problem 28
https://projecteuler.net/problem=28
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
从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
It can be verified that the sum of the numbers on the diagonals is 101.
对角线上的数字(粗体)之和等于101。
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
一个类似的1001*1001的螺旋的对角线上的数字之和为多少?
from math import pow row = 1001 matrix = row * row tot = [1] spiral = [i for i in range(2, matrix+1)] spiral = spiral[::-1] # 倒序排列螺旋 interval = 1 count = 0 while True: try: for i in range(interval): # 每隔interval个数字,加入一个数字至tot列表 spiral.pop() tot.append(spiral.pop()) count += 1 if count == 4: # 每加入四个数字,interval加二 interval += 2 count = 0 except: break print(sum(tot), tot)
Resistance is Futile!