





















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of recursion, a powerful problem-solving technique and algorithm used in computer science. Recursion is more than just a programming technique; it's a way of designing solutions to problems using the divide and conquer approach. In this document, we discuss the concept of recursion, its relationship with iterative definitions, and the differences between iterative and recursive methods. We also delve into the workings of recursion, its advantages and disadvantages, and its applications, such as reversing a list and calculating fibonacci numbers.
Typology: Slides
1 / 61
This page cannot be seen from the preview
Don't miss anything!






















































Answer: Answer:
factorial(n) = 1 if n= n x factorial(n-1) if n>
Function calls itself
Function does NOT calls itself
factorial(n) {
i = 1 factN = 1 loop (i <= n) factN = factN * i i = i + 1 end loop return factN
}
The iterative solution is very straightforward. We simply loop through all the integers between 1 and n and multiply them together.
factorial(n) {
if (n = 0) return 1 else return n*factorial(n-1) end if
}
Note how much simpler the code for the recursive version of the algorithm is as compared with the iterative version
we have eliminated the loop and implemented the algorithm with 1 ‘if’ statement. Docsity.com