This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< Chapter 5 Summary and Problems | Contents | 6.1 Recursive Functions >
Chapter 6. Recursion¶
Motivation¶
Imagine that a CEO of a large company wants to know how many people work for him. One option is to spend a tremendous amount of personal effort counting up the number of people on the payroll. However, the CEO has other more important things to do, and so implements another, more clever, option. At the next meeting with his department directors, he asks everyone to tell him at the next meeting how many people work for them. Each director then meets with all their managers, who subsequently meet with their supervisors who perform the same task. The supervisors know how many people work under them and readily report this information back to their managers (plus one to count themselves), who relay the aggregated information to the department directors, who relay the relevant information to the CEO. In this way, the CEO accomplishes a difficult task (for himself) by delegating similar, but simpler, tasks to his subordinates.
This method of solving difficult problems by breaking them up into simpler problems is naturally modeled by recursive relationships, which are the topic of this chapter, and which form the basis of important engineering and science problem-solving techniques. By the end of this chapter, you should be able to recognize recursive relationships and program them using recursive functions.