(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 8148, 339]*) (*NotebookOutlinePosition[ 8859, 364]*) (* CellTagsIndexPosition[ 8815, 360]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Different Ways to Define n!", "Subtitle", TextAlignment->Center, TextJustification->0], Cell[TextData[{ "There are usually many ways to attack a problem using ", StyleBox["Mathematica", FontSlant->"Italic"], ". As a case study, we consider the definition of a function f to \ calculate n!, the factorial of an integer n.\nCan you add to this list?\n\n\ Notes:\n - In each section, we use Clear[f] to remove the earlier definition \ of f.\n - We use ?f to reveal each definition of f." }], "Text"], Cell[CellGroupData[{ Cell["1. Set the name f equal to the built-in factorial function", "Subsubsection"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f\ = \ Factorial\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[4]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ 2. Define the function f using the built-in factorial function !\ \>", "Subsubsection"], Cell["Note the use of the underscore _ on the left hand side.", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ \(n!\)\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ 3. Define f using a different built-in function, the gamma function\ \>", "Subsubsection"], Cell["Note the capitalization of Gamma.", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := Gamma[n + 1]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["4. Define n! as the product of integers up to n", "Subsubsection"], Cell["Look up Product in the Help Browser to see details.", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := Product[i, {i, n}]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["5. Define n! recursively", "Subsubsection"], Cell["\<\ The function is automatically called repeatedly since in appears on the right \ side of the equation definition. The value n=1 is a special case (and is \ necessary to stop the recursion!); it is given in a separate statement. The semicolon separates two statements (and suppresses the output from the \ first).\ \>", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ n\ f[n - 1]; \ f[1] = 1\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ 6. Use an If statement to include the case n=1 in the recursive definition\ \>", "Subsubsection"], Cell["\<\ We can read the definition as \"If n=1, then f[n] equals 1, else f[ n] equals \ n time f[n-1].\" Note the double equal signs == used to make comparisons.\ \>", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ If[n == 1, 1, n\ f[n - 1]]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["7. Write a little program using a Do loop", "Subsubsection"], Cell["\<\ Look up Module and Do in the Help Browser to see details. The local variable t is declared in the first {}'s and initialized to 1. The \ Do loop runs through values of i from 1 to n and at each step multiplies the \ old value of t by i and sets this to be the new value of t. The final t means \ to return the value of t at the end.\ \>", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ Module[{t = 1}, \ Do[t = t*i, {i, n}]; \ t]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["8. Write a little C-style program using a For loop", "Subsubsection"], Cell["\<\ Look up Module and For in the Help Browser to see details. The local variables are t and i, and t is initialized to 1. The For loop \ starts with i=1 and continues as long as i is less than or equal to n. After \ each step, i is increased by 1 (this is what i++ does), and the value of t is \ multiplied by i and then stored again in t (this is what t*=i does). \ Finally, the value of t is returned.\ \>", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ Module[{t = 1, i}, \n \t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ For[i = 1, i <= n, \(i++\), t *= i]; \ t\n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ 9. Make a list of integers up to n, then use Apply and Times to multiply them\ \ \>", "Subsubsection"], Cell["\<\ Look up Apply, Times, and Range in the Help Browser to see details. Note that we can use @@ as an operator instead of Apply in the usual function \ form.\ \>", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ Apply[Times, Range[n]]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["\<\ 10. Make a list of integers up to n, then use Fold and Times to multiply them\ \ \>", "Subsubsection"], Cell["\<\ Look up Fold, Times, and Range in the Help Browser to see details.\ \>", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ Fold[Times, 1, Range[n]]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["11. For the experts!", "Subsubsection"], Cell["Can you figure out how this works?", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f\ = If[#1 == 1, 1, #1\ #0[#1 - 1]]&\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["12. Also for the experts!", "Subsubsection"], Cell["Can you figure out how this works?", "Text"], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[n_]\ := \ Fold[#2[#1]&, 1, Array[Function[t, # t]&, n]]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell[BoxData[ \(f[5]\)], "Input"] }, Open ]] }, Open ]] }, FrontEndVersion->"Microsoft Windows 3.0", ScreenRectangle->{{0, 800}, {0, 572}}, WindowSize->{466, 425}, WindowMargins->{{20, Automatic}, {Automatic, 5}}, PrintingCopies->1, PrintingPageRange->{Automatic, Automatic} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 96, 2, 68, "Subtitle"], Cell[1830, 55, 421, 8, 185, "Text"], Cell[CellGroupData[{ Cell[2276, 67, 86, 1, 51, "Subsubsection"], Cell[2365, 70, 41, 1, 35, "Input"], Cell[2409, 73, 50, 1, 35, "Input"], Cell[2462, 76, 39, 1, 35, "Input"], Cell[2504, 79, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2578, 85, 97, 2, 51, "Subsubsection"], Cell[2678, 89, 71, 0, 38, "Text"], Cell[2752, 91, 41, 1, 35, "Input"], Cell[2796, 94, 52, 1, 35, "Input"], Cell[2851, 97, 39, 1, 35, "Input"], Cell[2893, 100, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2967, 106, 100, 2, 51, "Subsubsection"], Cell[3070, 110, 49, 0, 38, "Text"], Cell[3122, 112, 41, 1, 35, "Input"], Cell[3166, 115, 56, 1, 35, "Input"], Cell[3225, 118, 39, 1, 35, "Input"], Cell[3267, 121, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3341, 127, 72, 0, 51, "Subsubsection"], Cell[3416, 129, 67, 0, 38, "Text"], Cell[3486, 131, 41, 1, 35, "Input"], Cell[3530, 134, 62, 1, 35, "Input"], Cell[3595, 137, 39, 1, 35, "Input"], Cell[3637, 140, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3711, 146, 49, 0, 51, "Subsubsection"], Cell[3763, 148, 336, 6, 143, "Text"], Cell[4102, 156, 41, 1, 35, "Input"], Cell[4146, 159, 69, 1, 35, "Input"], Cell[4218, 162, 39, 1, 35, "Input"], Cell[4260, 165, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[4334, 171, 107, 2, 70, "Subsubsection"], Cell[4444, 175, 177, 4, 80, "Text"], Cell[4624, 181, 41, 1, 35, "Input"], Cell[4668, 184, 72, 1, 35, "Input"], Cell[4743, 187, 39, 1, 35, "Input"], Cell[4785, 190, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[4859, 196, 66, 0, 51, "Subsubsection"], Cell[4928, 198, 357, 6, 122, "Text"], Cell[5288, 206, 41, 1, 35, "Input"], Cell[5332, 209, 89, 1, 56, "Input"], Cell[5424, 212, 39, 1, 35, "Input"], Cell[5466, 215, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5540, 221, 75, 0, 51, "Subsubsection"], Cell[5618, 223, 427, 7, 143, "Text"], Cell[6048, 232, 41, 1, 35, "Input"], Cell[6092, 235, 284, 6, 98, "Input"], Cell[6379, 243, 39, 1, 35, "Input"], Cell[6421, 246, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[6495, 252, 112, 3, 70, "Subsubsection"], Cell[6610, 257, 177, 4, 80, "Text"], Cell[6790, 263, 41, 1, 35, "Input"], Cell[6834, 266, 68, 1, 35, "Input"], Cell[6905, 269, 39, 1, 35, "Input"], Cell[6947, 272, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[7021, 278, 112, 3, 70, "Subsubsection"], Cell[7136, 283, 90, 2, 38, "Text"], Cell[7229, 287, 41, 1, 35, "Input"], Cell[7273, 290, 70, 1, 35, "Input"], Cell[7346, 293, 39, 1, 35, "Input"], Cell[7388, 296, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[7462, 302, 45, 0, 51, "Subsubsection"], Cell[7510, 304, 50, 0, 38, "Text"], Cell[7563, 306, 41, 1, 35, "Input"], Cell[7607, 309, 70, 1, 35, "Input"], Cell[7680, 312, 39, 1, 35, "Input"], Cell[7722, 315, 37, 1, 35, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[7796, 321, 50, 0, 51, "Subsubsection"], Cell[7849, 323, 50, 0, 38, "Text"], Cell[7902, 325, 41, 1, 35, "Input"], Cell[7946, 328, 92, 1, 56, "Input"], Cell[8041, 331, 39, 1, 35, "Input"], Cell[8083, 334, 37, 1, 35, "Input"] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)