Monday, October 12, 2009

Factorial Function

A repetitive algorithm uses recursion whenever the algorithm appears in the definition itself. Simplest example of a recursive definition is that for the factorial function; defined as

n! = {

1 if n = 0 base case

n* (n – 1) ! if n > 0 general case

The decomposition of Factorial(3) is shown in figure 1. It can be noted that the recursive solution for a problem involves a two-way journey: First we decompose the problem from top to the bottom, and then we solve it from bottom to the top. The recursive solution offers a conceptual simplicity to the creator and the reader.

No comments:

Post a Comment