The Magic Constant

EasyAcc. 98.5%
+20 XP 8

Celestial Harmony

A magic square of order $N$ is an $N imes N$ grid containing numbers $1, 2, dots, N^2$ such that the sum of each row, column, and diagonal is the same. This constant is called the Magic Constant. Formula: $M = n(n^2 + 1) / 2$.

The Assignment

Your function receives a parameter n.

  1. Use a loop to calculate the sum of numbers from 1 up to $n^2$.
  2. Divide that sum by n.
  3. Print the resulting Magic Constant.

01EXAMPLE 1

Inputn = 3
Output15

Explanation: 3(9+1)/2 = 15.

Constraints

  • Use a loop to find the total sum of N^2 elements.
  • Print the final integer.
MathLoops
JavaScript
Loading...
3 Hidden

Input Arguments

n3

Expected Output

15

Click RUN to test your solution