The factorial of a whole number n — which we denote by n! — may be defined as follows:
n! = |
{ |
1 |
|
if n = 0 |
n ⋅ (n – 1)! |
if n ≠ 0. |
In words: For each whole number n, n! (read "n-factorial") is 1 if n is zero, or n times (n – 1)! otherwise. Thus, for example, 5! = 120:
5! |
= 5 ⋅ 4!
= 5 ⋅ (4 ⋅ 3!)
= 5 ⋅ (4 ⋅ (3 ⋅ 2!))
= 5 ⋅ (4 ⋅ (3 ⋅ (2 ⋅ 1!)))
= 5 ⋅ (4 ⋅ (3 ⋅ (2 ⋅ (1 ⋅ 0!))))
= 5 ⋅ (4 ⋅ (3 ⋅ (2 ⋅ (1 ⋅ 1))))
= 120. |
Value: 10 points.
Using the Scheme Evaluator provided, write and test a definition for the function factorial that inputs a whole number n and outputs n!.
Problem Set 7 (13)
-
Values will appear here.
Press to run some simple built-in test cases.
Test results will appear here.
|
|
Alert: The function factorial is a useful one to include in your Scheme library. Add it to your Numbers file. After doing so, return here and check that your library is up to date and functioning correctly:
Press this button to test your Scheme library: