PlanetMath (more info)
 Math for the people, by the people. Sponsor PlanetMath
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: High Entry average rating: No information on entry rating
[parent] recursive algorithm for factorial function (Algorithm)

When it came to teaching recursion in programming languages in the 1980s and 1990s, the factorial function $n!$ was the classic example to explain the concept.

Usually left unexplained, in a mathematical paper or book one might encounter an explanation for the $n!$ shorthand for this function along the lines of $$n! = \prod_{i = 1}^n i,$$ (with $0! = 1$ ) while a computer programming book might say something like $n! = 1 \times 2 \times \ldots \times (n - 1) \times n$ (with the same assignment for zero factorial). Either of these suggests implementation with some kind of FOR loop.

The recurrence relation $n! = (n - 1)!n$ with $n > 1$ and $1! = 1$ suggests a recursive implementation.

Call the recursive factorial algorithm with an integer N.

  1. Test if N <= 0. If so, return 1.
  2. If not, then call the recursive factorial algorithm with N - 1, multiply the result by N and return that value.

The algorithm calls itself and some mechanism is necessary for keeping track of the state of the computation. Here's an implementation in the PASCAL programming language from Koffman's 1981 book:

FUNCTION FACTORIAL (N: INTEGER): INTEGER
(* RECURSIVE COMPUTATION OF N FACTORIAL *)

BEGIN
(* TEST FOR STOPPING STATE *)
IF N <= 0 THEN
FACTORIAL := 1
ELSE
FACTORIAL := N * FACTORIAL(N - 1)
END; (* FACTORIAL *)

Depending on the implementation, what would happen the first time FACTORIAL(N) calls itself is that the memory address of the function together with $n - 1$ would be pushed on to the stack. The next time $n - 2$ would be pushed on the stack, and so on and so forth until 0 is reached. At this point, the last instance of the function returns, the next-to-last instance pops a 1 off the stack and multiplies it by 2, the next-to-next-to-last instance pops a 2 off the stack and multiplies it by 3, pushes a 6, and so on and so forth until the first instance pops $(n - 1)!$ off the stack and multiplies it by $n$ .

The following table illustrates a sample run starting with N = 7:

Action N Return value
Call factorial function with value 7 Undefined
Call factorial function with value 6 Undefined
Call factorial function with value 5 Undefined
Call factorial function with value 4 Undefined
Call factorial function with value 3 Undefined
Call factorial function with value 2 Undefined
Call factorial function with value 1 Undefined
Call factorial function with value 0  
Return a 1   1
Multiply returned value by N and return that 1 1
Multiply returned value by N and return that 2 2
Multiply returned value by N and return that 3 6
Multiply returned value by N and return that 4 24
Multiply returned value by N and return that 5 120
Multiply returned value by N and return that 6 720
Multiply returned value by N and return that 7 5040

This kind of recursion can exhaust memory (for stack space) well before any computations are performed. However, in this specific application, because factorials grow super exponetially, the bounding for integer capacity is usually far more restricting than the memory capacity. For example, using 32-bit unsigned integers and guesstimating each function call requires 16 bytes, the computation of 13! would require just 208 bytes on the stack, but the result would require 33 bits, overflowing a 32-bit unsigned integer variable. Therefore input sizes should be limited to fit within the bounds of memory and integer capacity.

Bibliography

1
B. Allan, Introducing Pascal. London: Granada (1984): 56 - 57
2
J. G. P. Barnes, Programming in ADA. Wokingham: Addison-Wesley (1989): 117
3
A. I. Hollub, The C Companion. Englewood Cliffs: Prentice-Hall, Inc. (1987): 190 - 195
4
R. Kemp, PASCAL for Students. London: Edward Arnold (1987): 106
5
E. Koffman, Problem Solving and Structured Programming in PASCAL. Reading: Addison-Wesley Publishing Company (1981): 317




"recursive algorithm for factorial function" is owned by PrimeFan.
(view preamble | get metadata)

View style:


This object's parent.
Log in to rate this entry.
(view current ratings)

Cross-references: bounds, sizes, variable, capacity, application, action, pops, stack, necessary, integer, algorithm, recursive, recurrence relation, loop, computer, lines, function, factorial function, languages

This is version 2 of recursive algorithm for factorial function, born on 2007-07-31, modified 2007-08-03.
Object id is 9824, canonical name is RecursiveAlgorithmForFactorialFunction.
Accessed 7105 times total.

Classification:
AMS MSC05A10 (Combinatorics :: Enumerative combinatorics :: Factorials, binomial coefficients, combinatorial functions)
 11B65 (Number theory :: Sequences and sets :: Binomial coefficients; factorials; $q$-identities)

Pending Errata and Addenda
None.
[ View all 1 ]
Discussion
Style: Expand: Order:
forum policy

No messages.

Interact
post | correct | update request | add example | add (any)