The Spiral Sum

HardAcc. 75.2%
+40 XP 20

Walking the Spiral

Imagine an $N imes N$ matrix filled with numbers $1, 2, dots, N^2$ in a spiral order starting from the top-left. In this mission, you don't need a matrix—calculate the sum of values on the diagonals via a loop.

The Assignment

Your function receives a parameter size (an odd number).

  1. Initialize totalSum = 1.
  2. Use a loop from 3 up to size (step 2).
  3. In each shell, the four corners are k^2, k^2 - (k-1), k^2 - 2(k-1), and k^2 - 3(k-1).
  4. Add these to totalSum and print the final result.

01EXAMPLE 1

Inputsize = 3
Output25

Explanation: Corners: 3, 5, 7, 9. Sum = 1+3+5+7+9 = 25.

Constraints

  • Size will always be odd.
  • Only use loops, no arrays.
MathLoops
JavaScript
Loading...
2 Hidden

Input Arguments

size3

Expected Output

25

Click RUN to test your solution