Abstract
Consider again the function n!. We have already seen how to compute it with a loop that multiplies the numbers 1 through n. Another way to compute n! is with a recursive program that calls itself:
Because\( n! = n * \left( {n - 1} \right)! \), it can be computed by solving a smaller version of the same problem and then multiplying by n. In this very simple example, the recursive program corresponds directly to a loop. If the recursion is "unwound", to compute n!, we compute \( \left( {n - 1} \right)! \), to compute \( \left( {n - 1} \right)! \) we compute \( \left( {n - 2} \right)! \), and so on until we get down to 1; then we work backwards computing \( 2 * 1 = 2 \), then \( 3 * 2 = 6 \), then \( 4 * 6 = 24 \), and so on. This form of recursion, often called tail recursion, calls itself on the "tail" of the problem and then does some simple computation (in this case, a multiplication) to incorporate the "head" of the problem. In fact, it is such a simple form of recursion that a "smart" compiler can detect it and translate it to a loop that uses O(1) space.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2002 J.A. Storer
About this chapter
Cite this chapter
Storer, J.A. (2002). Induction and Recursion. In: An Introduction to Data Structures and Algorithms. Birkhäuser, Boston, MA. https://doi.org/10.1007/978-1-4612-0075-8_3
Download citation
DOI: https://doi.org/10.1007/978-1-4612-0075-8_3
Publisher Name: Birkhäuser, Boston, MA
Print ISBN: 978-1-4612-6601-3
Online ISBN: 978-1-4612-0075-8
eBook Packages: Springer Book Archive