Sum of First N Numbers¶
Description¶
Write a recursive function that computes the sum of all integers from 1 up to a given non-negative integer n.
Test Cases
Example 1: Input: n = 5 Output: 15 Explanation: 5 + 4 + 3 + 2 + 1 + 0 = 15
Example 2: Input: n = 10 Output: 55 Explanation: 10 + 9 + ... + 1 + 0 = 55
Example 3: Input: n = 0 Output: 0
Approach 1: Recursion¶
- Time Complexity: O(n) - The function calls itself
ntimes. - Space Complexity: O(n) - Each function call is placed on the call stack, leading to
nstack frames in memory at the deepest point.
This function works based on a recursive definition of sum:
- Base Case: The sum of numbers up to 0 is defined as 0. This stops the recursion.
- Recursive Step: The sum of numbers up to
nisnplus the sum of numbers up ton-1.
The function continuously calls itself with a decreasing argument (n-1) until it reaches the base case (n === 0), at which point the results are returned back up the call stack.