Functions

/Users/garethloy/Musimathics/Musimat1.2/MusimatTutorial/B0125.cpp File Reference

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0125)
Integer recurFactorial (Integer x)
Static Void para1 ()

Function Documentation

MusimatTutorialSection ( B0125   )

Definition at line 2 of file B0125.cpp.

References para1().

                              {
        Print("*** B.1.25 Recursive Factorial ***");
        /*****************************************************************************
         
         B.1.25 Recursive Factorial
         
         Here is a more direct approach to computing factorials using recursion. 
         In standard mathematics, the "!" operator means "factorial". (This usage is
         in conflict with standard programming languages, which typically don't have
         a factorial operator.  Further, Musimat has taken over the "!" operator to mean "not".
         
         Using the mathematical form of "!" meaning factorial,  
                x! = x * (x - 1)!, 
         which in English is "x factorial equals x times x minus 1 the quantity factorial."
         We can write a function that calculates factorial using this expression, as shown below
         in the function recurFactorial().
         *****************************************************************************/
        
        para1(); // Step into this function to continue the tutorial
}
Static Void para1 (  )

Definition at line 30 of file B0125.cpp.

References recurFactorial().

                    {
        
        /*****************************************************************************
         This method has two states. If x == 1, we return 1 since 1! is equal to 1. Otherwise, we return 
         x multiplied by the factorial of x - 1. Consider the statement
         *****************************************************************************/
        
        Print(recurFactorial(5));
        
        /*****************************************************************************
         When the recurFactorial function is called, x is assigned the value 5. Because 5 is not equal to 1, the fac-
         torial function evaluates the Else statement and calls recurFactorial(4). Because 4 is not equal 
         to 1, recurFactorial evaluates the Else statement and calls recurFactorial(3), and so on. 
         Eventually, we reach recurFactorial(1), which returns 1, which is multiplied by 2, then by 3, then 
         by 4, and finally by 5. The top-level recurFactorial() function returns the product, 120, to the 
         Print() routine.
         
         *****************************************************************************/
}
Integer recurFactorial ( Integer  x )

Definition at line 23 of file B0125.cpp.

                                 {
        If (x == 1) 
                Return(1);
        Else 
                Return(x * recurFactorial(x - 1));
}