Code
// Factorial RecursionAlgorithm(A combination of N numbers)
Function Calfactorial (N)
{
If (N < 0 )
{
Return ' N cannot be negative. ' ;
}
Else If (N = 0 )
{
Return 1 ;
}
Else
{
Return N * Calfactorial (n - 1 );
}
}
Function Factorialwork (anumber, recursnumber ){
// Recursnumber keeps track of the number of iterations so far.
If (Anumber = 0 ){ // If the number is 0, its factorial is 1.
Return 1 .;
} Else {
If (Recursnumber > 100 ){
Return ( " Too many levels of recursion. " );
} Else { // Otherwise, recurse again.
Return (Anumber * Factorialwork (anumber - 1 , Recursnumber + 1 ));
}
}
}
Function Factorial (anumber ){
// Use Type annotation to only accept numbers coercible to integers.
// Double is used for the return type to allow very large numbers to be returned.
If (Anumber < 0 ){
Return ( " Cannot take the factorial of a negative number. " );
} Else { // Call the recursive function.
Return Factorialwork (anumber, 0 );
}
}
//Call the factorial function for two values.
Alert (factorial (5));
Alert (factorial (80));