Skip to main content
  • 1248 Accesses

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:

$$ \begin{gathered} function FACT(n) \hfill \\ if n \leqslant 1 \hfill \\ then return 1 \hfill \\ else return n * FACT\left( {n - 1} \right) \hfill \\ end \hfill \\ \end{gathered} $$

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info
Hardcover Book
USD 54.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics