Beginner Level

Implement the method sumOfSquares that returns the sum of squares from 1 to n.

Return the sum 1^2 + 2^2 + ... + n^2. For example, when n = 3, the result is 14.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Return 0 when n is 0.
  • Inputs are non-negative.
  • A loop or the mathematical formula may be used.
Example 1
Input:
n (int) = 3
Return:
(int) 14
Example 2
Input:
n (int) = 5
Return:
(int) 55
Example 3
Input:
n (int) = 1
Return:
(int) 1
Example 4
Input:
n (int) = 0
Return:
(int) 0

Add i * i for every number from 1 through n, or apply the known sum-of-squares formula.

Run your code to see the result.