Project Euler/28
From charlesreid1
Contents
Spiral Diagonals Sum Analysis
This page provides a mathematical analysis and algorithm to compute the sum of numbers on the diagonals of an spiral grid, starting from 1 at the center and moving clockwise. The problem is to find the sum for a spiral, given that the sum for a spiral is 101.
Understanding the 5×5 Spiral
Consider the spiral provided as an example:
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
The diagonals are:
- Main diagonal (top-left to bottom-right): 21, 7, 1, 3, 13.
- Anti-diagonal (top-right to bottom-left): 25, 9, 1, 5, 17.
The sum of these numbers is:
- Main diagonal:
- Anti-diagonal:
Total: . Since the center (1) is counted twice, subtract it once: , which matches the given sum.
Analyzing the Spiral Pattern
The spiral starts at 1 in the center and moves outward in layers, clockwise. For a grid:
- Layer 0: Center (1).
- Layer 1: grid (numbers 2 to 9).
- Layer 2: grid (numbers 10 to 25).
For an grid where is odd (), the number of layers is:
- , so , .
- , so , .
Each layer forms a square of side length . The numbers on the diagonals are the corners of these layers.
Number of Elements per Layer
- Layer 0: 1 number.
- Layer 1: numbers, minus the center, so 8 numbers.
- Layer 2: numbers, minus the inner (9 numbers), so numbers.
For layer , the side length is , so the grid has numbers. The inner grid (layer ) has numbers. The numbers in layer :
Total numbers up to layer :
The last number in layer (top-right corner) is .
Corner Numbers on Diagonals
For layer , the corners are:
- Top-right:
- Top-left:
- Bottom-left:
- Bottom-right:
Verify for ():
- Layer 1 ():
- Top-right:
- Top-left:
- Bottom-left:
- Bottom-right:
- Layer 2 (Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle m=2}
):
- Top-right: Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle (2\times 2+1)^{2}=25}
- Top-left: Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle 25-2\times 2=21}
- Bottom-left: Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle 25-4\times 2=17}
- Bottom-right: Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle 25-6\times 2=13}
These match the grid.
Sum of Diagonals for 1001×1001 Spiral
For , . Sum the corners from layer 1 to 500, plus the center (1).
Sum of corners in layer :
Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle 4(2m+1)^{2}-(2m+4m+6m)=16m^{2}+4m+4}
Total sum from layer 1 to 500:
Using:
- Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle \sum _{m=1}^{n}m^{2}={\frac {n(n+1)(2n+1)}{6}}}
For :
- Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle \sum _{m=1}^{500}m={\frac {500\cdot 501}{2}}=125250}
- Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle \sum _{m=1}^{500}1=500}
Total:
Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle 16\cdot 41791750+4\cdot 125250+4\cdot 500=669171000}
Add the center: Failed to parse (Conversion error. Server ("https://en.wikipedia.org/api/rest_") reported: "Cannot get mml. Server problem."): {\displaystyle 669171000+1={\mbox{the final answer}}} .
Algorithm to Compute the Sum
Below is a pseudocode algorithm to compute the sum efficiently:
function sumDiagonals(n):
k = (n - 1) / 2
sum = 1 # Center
for m from 1 to k:
sum += 16 * m * m + 4 * m + 4
return sum
n = 1001
result = sumDiagonals(n)
print(result)
This algorithm avoids generating the entire spiral, focusing on the diagonal numbers.
Final Answer
The sum of the numbers on the diagonals of a spiral is
Flags