Download presentation
Presentation is loading. Please wait.
Published byMoses Carter Modified over 9 years ago
1
“The coolest phones we’ve seen from the world’s largest mobile show so far include quad-core chips, larger screens, and even one with a projector built in.” http://www.pcworld.com/article/250726/mobile_world_congress_2012_first_wa ve_of_smartphones.html#tk.hp_pop The Beauty and Joy of Computing Lecture #11 Recursion II UC Berkeley iSchool Grad Student Dan Garcia
2
UC Berkeley “The Beauty and Joy of Computing” : Recursion II (2) Garcia Factorial(n) = n! Inductive definition: n! = 1, n = 0 n! = n * (n-1)!, n > 0 Let’s act it out… “Little people”, or “subcontractor” model 5! How the Computer Works … n! nn! 01 11 22 36 424 5120
3
UC Berkeley “The Beauty and Joy of Computing” : Recursion II (3) Garcia Inductive definition: fib(n) = n, n < 2 fib(n) = fib(n-1)+fib(n-2), n > 1 Let’s act it out… “contractor” model fib(5) How the Computer Works … fib(n) nfib(n) 00 11 21 32 43 55 Leonardo de Pisa aka, Fibonacci en.wikipedia.org/wiki/Fibonacci_number www.ics.uci.edu/~eppstein/161/960109.html Let’s now: trace… (gif from Ybungalobill@wikimedia)
4
UC Berkeley “The Beauty and Joy of Computing” : Recursion II (4) Garcia Given coins {50, 25, 10, 5, 1} how many ways are there of making change? 5: 2 (N,5 P) 10 4 (D, 2N, N 5P, 10P) 15 6 (DN,D5P,3N,2N5P,1N10 P,15P) 100? Counting Change (thanks to BH)
5
UC Berkeley “The Beauty and Joy of Computing” : Recursion II (5) Garcia Call Tree for “Count Change 10 (10 5 1)” 10 (10 5 1) 10 (5 1) 10 (1)10 ()09 (1)9 ()08 (1)8 ()07 (1)7 ()06 (1)6 ()05 (1)5 ()04 (1)4 ()03 (1)3 ()02 (1)2 ()01 (1)1 ()00 (1)15 (5 1)5 (1) 5 ()04 (1)4 ()03 (1)3 ()02 (1)2 ()01 (1)1 ()00 (1)1 0 (5 1)1 0 (10 5 1) 1 Skip Coin Use Coin D NN N 5P 10P D? N? N? P? P? P? P? P? P? P? P? P? P? P? P? P? P? P?
6
UC Berkeley “The Beauty and Joy of Computing” : Recursion II (6) Garcia It’s important to understand the machine model It’s often the cleanest, simplest way to solve many problems Esp those recursive in nature! Recursion is a very powerful idea, and one way to separate good from great Summary Menger Cube by Dan Garcia
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.