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
0whennis0. - Inputs are non-negative.
- A loop or the mathematical formula may be used.
Example 1
Input
n (int) =
3Returns (int)
14Example 2
Input
n (int) =
5Returns (int)
55Example 3
Input
n (int) =
1Returns (int)
1Example 4
Input
n (int) =
0Returns (int)
0Add i * i for every number from 1 through n, or apply the known sum-of-squares formula.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run