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).
- Initialize
totalSum = 1. - Use a loop from 3 up to
size(step 2). - In each shell, the four corners are
k^2,k^2 - (k-1),k^2 - 2(k-1), andk^2 - 3(k-1). - Add these to
totalSumand print the final result.
01EXAMPLE 1
Input
size = 3Output
25Explanation: Corners: 3, 5, 7, 9. Sum = 1+3+5+7+9 = 25.
Constraints
- Size will always be odd.
- Only use loops, no arrays.
MathLoops
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
size3
Expected Output
25
Click RUN to test your solution