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. 00009 In standard mathematics, the "!" operator means "factorial". (This usage is 00010 in conflict with standard programming languages, which typically don't have 00011 a factorial operator. Further, Musimat has taken over the "!" operator to mean "not". 00012 00013 Using the mathematical form of "!" meaning factorial, 00014 x! = x * (x - 1)!, 00015 which in English is "x factorial equals x times x minus 1 the quantity factorial." 00016 We can write a function that calculates factorial using this expression, as shown below 00017 in the function recurFactorial(). 00018 *****************************************************************************/ 00019 00020 para1(); // Step into this function to continue the tutorial 00021 } 00022 00023 Integer recurFactorial(Integer x){ 00024 If (x == 1) 00025 Return(1); 00026 Else 00027 Return(x * recurFactorial(x - 1)); 00028 } 00029 00030 Static Void para1() { 00031 00032 /***************************************************************************** 00033 This method has two states. If x == 1, we return 1 since 1! is equal to 1. Otherwise, we return 00034 x multiplied by the factorial of x - 1. Consider the statement 00035 *****************************************************************************/ 00036 00037 Print(recurFactorial(5)); 00038 00039 /***************************************************************************** 00040 When the recurFactorial function is called, x is assigned the value 5. Because 5 is not equal to 1, the fac- 00041 torial function evaluates the Else statement and calls recurFactorial(4). Because 4 is not equal 00042 to 1, recurFactorial evaluates the Else statement and calls recurFactorial(3), and so on. 00043 Eventually, we reach recurFactorial(1), which returns 1, which is multiplied by 2, then by 3, then 00044 by 4, and finally by 5. The top-level recurFactorial() function returns the product, 120, to the 00045 Print() routine. 00046 00047 *****************************************************************************/ 00048 } 00049 00051 /* $Revision: 1.3 $ $Date: 2006/09/05 08:03:08 $ $Author: dgl $ $Name: $ $Id: B0125.cpp,v 1.3 2006/09/05 08:03:08 dgl Exp $ */ 00052 // The Musimat Tutorial © 2006 Gareth Loy 00053 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00054 // and published exclusively by The MIT Press. 00055 // This program is released WITHOUT ANY WARRANTY; without even the implied 00056 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00057 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00058 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00059 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00060 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00061 // The Musimat website: http://www.musimat.com/ 00062 // This program is released under the terms of the GNU General Public License 00063 // available here: http://www.gnu.org/licenses/gpl.txt 00064