














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
Recursive Functions. Recall factorial function: Iterative Algorithm. Loop construct (while) can capture computation in a set of state variables that.
Typology: Schemes and Mind Maps
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Execution trace for n = 4 fact (4) 4 * fact (3) 4 * (3 * fact (2)) 4 * (3 * (2 * fact (1))) 4 * (3 * (2 * (1 * fact (0)))) 4 * (3 * (2 * (1 * 1))) 4 * (3 * (2 * 1)) 4 * (3 * 2) 4 * 6 24 Courtesy Prof PR Panda CSE Department IIT Dehi
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
gcd (a, b) = b if a mod b = 0 gcd (b, a mod b) otherwise gcd (6, 10) gcd (10, 6) gcd (6, 4) gcd (4, 2) 2 2 2 2 Courtesy Prof PR Panda CSE Department IIT Dehi
fib (n) = 0 n = 1 1 n = 2 fib (n-1) + fib (n-2) n > 2 Recursive Algorithm Courtesy Prof PR Panda CSE Department IIT Dehi
power(x,n) = 1 if n = 0 x * power(x,n-1) otherwise
Origin Spare (^) Final Origin Spare (^) Final Origin Spare (^) Final Origin Spare (^) Final Solve for (n-1) disks. Move to Spare. Use Final as Spare. Move bottom disk to Final Move (n-1) disks to Final. Use Origin as Spare. Courtesy Prof PR Panda CSE Department IIT Dehi