It can be verified that the sum of the numbers on the diagonals is
. What is the sum of the numbers
on the diagonals in a by spiral formed in the same way?
# 1~N/2+1:从内向外的圈数。 for i inrange(1, N // 2 + 2): ans += 4 * i * i - 10 * i + 7 ans += 4 * (i - 1) * (i - 1) + 1 ans += 4 * i * i - 6 * i + 3 ans += (2 * i - 1) ** 2 # 中心点算重复了3次,故减去。 ans -= 3 print(ans)
1 2 3 4
N = 1001 N = N // 2 + 1 ans = (8 * N ** 3 - 9 * N ** 2 + 7 * N) * 2 // 3 - 3 print(ans)