742. Sum of Squares of First N Numbers

EasyRecursionRecursion

Given a non-negative integer n, return the sum 1^2 + 2^2 + 3^2 + ... + n^2. If n = 0, return 0. Implement the calculation recursively. The result fits in a 64-bit signed integer.

Input: A single non-negative integer n.

Output: Return an integer equal to the sum of squares from 1 to n.

Examples

Example 1
Input: 3
Output: 14
Explanation: 1 + 4 + 9 = 14.
Example 2
Input: 5
Output: 55
Explanation: 1 + 4 + 9 + 16 + 25 = 55.
Example 3
Input: 0
Output: 0
Explanation: Empty sum is 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →