





















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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