![]() ![]() Return n+smallerSum // solve bigger problem using solved smaller sub-problems Int smallerSum=sum(n-1) //recursive call for smaller problem Ques: Calculate the sum of n consecutive natural number starting with 1. Let’s try to understand it with an example: Computation of bigger problem using solved sub-problems.In both the above examples we saw the never-ending sub-problems (the mirrors will keep reflecting one another and there appears to be an infinite number of mirrors and in the second example, the figure will keep growing infinitely).īy this, we understand the need of having an end condition for every recursive function which will avoid this infinite structure. Can you imagine the resultant figure? Pin If we define a function to draw a triangle on its every edge. Now let’s try to visualize how recursion works: This happens because a mirror is reflecting a mirror, which is reflecting a mirror,…and so on. Think of a situation when you put a mirror in front of a mirror? ![]() Difference Between Direct and Indirect Recursion. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |