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
Returns (int) 14

Example 2

Input
n (int) = 5
Returns (int) 55

Example 3

Input
n (int) = 1
Returns (int) 1

Example 4

Input
n (int) = 0
Returns (int) 0

Add 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.