When it comes to programming, recursion is a powerful technique that allows a function to call itself. In this article, we will explore how to use recursion to find the sum from 1 to a given number, specifically N=10. So, let's dive in and understand how this recursive program works!
To find the sum from 1 to N=10 using recursion, we need to create a function that calls itself repeatedly until it reaches the base case. In this case, the base case is when N=1. Let's start by writing the function in Python:
def recursive_sum(n):
if n == 1:
return 1
else:
return n + recursive_sum(n - 1)
Let's break down the code and understand how it works:
- The function
recursive_sumtakes a parameternwhich represents the number we want to find the sum up to. - Inside the function, we have an
ifstatement that checks ifnis equal to 1. If it is, we return 1 as the sum from 1 to 1 is 1. - If
nis not equal to 1, we use recursion to find the sum from 1 ton-1by calling therecursive_sumfunction withn-1as the argument. We then addnto the result of the recursive call. - Finally, the function returns the sum.
Now that we have defined our recursive function, let's test it by finding the sum from 1 to N=10:
sum_10 = recursive_sum(10)
print("The sum from 1 to 10 is:", sum_10)
If you run the code, you will see the output:
The sum from 1 to 10 is: 55
Great! Our recursive program successfully found the sum from 1 to N=10, which is 55.
Recursion can be a bit tricky to understand at first, so let's visualize how the recursive calls are made for N=10:
| Function Call | Returned Value |
|---|---|
| recursive_sum(10) | 10 + recursive_sum(9) |
| recursive_sum(9) | 9 + recursive_sum(8) |
| recursive_sum(8) | 8 + recursive_sum(7) |
| recursive_sum(7) | 7 + recursive_sum(6) |
| recursive_sum(6) | 6 + recursive_sum(5) |
| recursive_sum(5) | 5 + recursive_sum(4) |
| recursive_sum(4) | 4 + recursive_sum(3) |
| recursive_sum(3) | 3 + recursive_sum(2) |
| recursive_sum(2) | 2 + recursive_sum(1) |
| recursive_sum(1) | 1 |
As you can see, each recursive call adds the current number to the sum of the previous numbers. When the base case is reached (N=1), the recursion stops and the sum is calculated.
It's important to note that recursion can be memory-intensive for large values of N, as each function call adds a new frame to the call stack. In such cases, an iterative approach might be more efficient. However, for small values of N, recursion provides an elegant solution.
Now that you understand how to find the sum from 1 to N=10 using recursion, you can apply this concept to solve similar problems. Just remember to define the base case and make recursive calls to solve smaller subproblems.
We hope this article has helped you understand the recursive program to find the sum from 1 to N=10. If you have any further questions, feel free to reach out to our tech support team for assistance.
References
| Author | Article |
|---|---|
| GeeksforGeeks | Recursive Program to Find the Sum of Digits |
| Tutorialspoint | Python - Recursion |
| Programiz | Python Recursion |