- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatTutorial/B0125.cpp

Go to the documentation of this file.

    00001 #include "MusimatTutorial.h"
    00002 MusimatTutorialSection(B0125) {
    00003 Print("*** B.1.25 Recursive Factorial ***");
    00004 /*****************************************************************************
    00005
    00006 B.1.25 Recursive Factorial
    00007
    00008 Here is a more direct approach to computing factorials using recursion. Since
    00009         x! = x * (x - 1)!,
    00010 we can write a function that uses this expression directly, as shown below
    00011 in the function recurFactorial().
    00012 *****************************************************************************/
    00013                 para1(); // Step into this function to continue the tutorial
    00014 }
    00015
    00016 Integer recurFactorial(Integer x){
    00017         If (x == 1)
    00018                 Return(1);
    00019         Else
    00020                 Return(x * recurFactorial(x - 1));
    00021 }
    00022
    00023 Static Void para1() {
    00024 /*****************************************************************************
    00025 This method has two states. If x == 1, we return 1 since 1! is equal to 1. Otherwise, we return
    00026 x multiplied by the factorial of x – 1. Consider the statement
    00027 *****************************************************************************/
    00028
    00029 Print(recurFactorial(5));
    00030
    00031 /*****************************************************************************
    00032 When the recurFactorial function is called, x is assigned the value 5. Because 5 is not equal to 1, the fac-
    00033 torial function evaluates the Else statement and calls recurFactorial(4). Because 4 is not equal
    00034 to 1, recurFactorial evaluates the Else statement and calls recurFactorial(3), and so on.
    00035 Eventually, we reach recurFactorial(1), which returns 1, which is multiplied by 2, then by 3, then
    00036 by 4, and finally by 5. The top-level recurFactorial() function returns the product, 120, to the
    00037 Print() routine.
    00038
    00039 *****************************************************************************/
    00040 }}
    00041
    00043 /* $Revision: 1.1 $ $Date: 2006/09/08 19:11:32 $ $Author: dgl $ $Name:  $ $Id: _b0125_8cpp-source.html,v 1.1 2006/09/08 19:11:32 dgl Exp $ */
    00044 // The Musimat Tutorial © 2006 Gareth Loy
    00045 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00046 // and published exclusively by The MIT Press.
    00047 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00048 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00049 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00050 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00051 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00052 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00053 // The Musimat website:                 http://www.musimat.com/
    00054 // This program is released under the terms of the GNU General Public License
    00055 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00056