Given an integer N, return the sum of first N natural numbers. Try to solve this using recursion.
Input : N = 4
Output : 10
Explanation : first four natural numbers are 1, 2, 3, 4.
Sum is 1 + 2 + 3 + 4 => 10.
Input : N = 2
Output : 3
Explanation : first two natural numbers are 1, 2.
Sum is 1 + 2 => 3.
Input : N = 10
To find the sum of the first N numbers, the concept of recursion can be utilized by breaking down the problem into smaller subproblems. This involves repeatedly adding the current number to the sum of all previous numbers. The process continues until reaching the base case, where N is 0, which terminates the recursion.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int NnumbersSum(int N) {
// Base case: if N is 0, return 0
if (N == 0) return 0;
// Recursive case: add N to the sum of N-1
return N + NnumbersSum(N - 1);
}
};
int main() {
Solution solution;
int N = 10; // Example input
cout << "Sum of first " << N << " numbers is " << solution.NnumbersSum(N) << endl;
return 0;
}
class Solution {
public int NnumbersSum(int N) {
// Base case: if N is 0, return 0
if (N == 0) return 0;
// Recursive case: add N to the sum of N-1
return N + NnumbersSum(N - 1);
}
public static void main(String[] args) {
Solution solution = new Solution();
int N = 10; // Example input
System.out.println("Sum of first " + N + " numbers is " + solution.NnumbersSum(N));
}
}
class Solution:
def NnumbersSum(self, N):
# Base case: if N is 0, return 0
if N == 0:
return 0
# Recursive case: add N to the sum of N-1
return N + self.NnumbersSum(N - 1)
if __name__ == "__main__":
solution = Solution()
N = 10 # Example input
print(f"Sum of first {N} numbers is {solution.NnumbersSum(N)}")
class Solution {
NnumbersSum(N) {
// Base case: if N is 0, return 0
if (N === 0) return 0;
// Recursive case: add N to the sum of N-1
return N + this.NnumbersSum(N - 1);
}
}
const solution = new Solution();
const N = 10; // Example input
console.log(`Sum of first ${N} numbers is ${solution.NnumbersSum(N)}`);
Time Complexity O(N) — The function makes N recursive calls to reach the base case, so the time complexity is proportional to the number of calls made
Space Complexity O(N) — In the worst case, the recursion stack space would be full with all the function calls waiting to get completed and that would make it an O(N) recursion stack space