| |||||||||||
|
| |||||||||||||||
| FACT(n, total)
:= Prog total := n Loop If n = 2 RETURN total total :* n - 1 |
As an exercise you may like to compare the computation times of FACT(1000) using the definitions with and without the use of update operators.
The FACT(n) program is still not complete, as it will only work if n is a positive integer. A user may well try to use a value other than an integer for n. Clearly this would cause an infinite loop. Also, 0! And 1! are defined as 1, so FACT(n) is not watertight at the moment. The 0! Issue is easily addressed by adding the lines IF(n=0, RETURN 1), IF(n=1, RETURN 1) at the beginning. The issue of n being a positive integer can be addressed easily when we look at predicate functions for recognising the data type of expressions. However, even without these functions we can still tighten up the definition of FACT(n). But before we do this we will introduce the Boolean operators.
Move on to Boolean Operators
Return to Programming Basics