00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0126) { 00003 Print("*** B.1.26 Fibonacci Numbers ***"); 00004 /***************************************************************************** 00005 00006 B.1.26 Fibonacci Numbers 00007 00008 In the sequence 00009 00010 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 377, 610, 987, 1597, 2584, . . . 00011 00012 each subsequent term is the sum of its two immediately preceding values. For example, 8 = 5 + 3. 00013 This series, invented by Leonardo Pisano Fibonacci (1170–1250), is the solution to a problem he 00014 posed in his book "Liber Abaci": “A certain man put a pair of rabbits in a place surrounded on 00015 all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is 00016 supposed that every month each pair begets a new pair which from the second month on becomes 00017 productive?” 00018 00019 An iterative method of computing the Fibonacci sequence is shown below 00020 in function iterFib(). 00021 *****************************************************************************/ 00022 para1(); // Step into this function to continue the tutorial 00023 para2(); // Step into this function to continue the tutorial 00024 } 00025 00026 Integer iterFib(Integer n) { 00027 Integer fn1 = 1; 00028 Integer fn2 = 1; 00029 Integer result = 1; 00030 00031 For (Integer i = 2; i < n; i = i + 1) { 00032 result = fn1 + fn2; 00033 fn2 = fn1; 00034 fn1 = result; 00035 } 00036 Return (result); 00037 } 00038 00039 Static Void para1() { 00040 /***************************************************************************** 00041 Executing the following For loop, 00042 *****************************************************************************/ 00043 00044 For ( Integer i = 1; i < 10; i = i + 1 ) 00045 Print(iterFib(i)); 00046 } 00047 00048 /***************************************************************************** 00049 prints the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34. 00050 Below is a method that accomplishes the same calculation using recursion: 00051 *****************************************************************************/ 00052 00053 Integer recurFib(Integer n) { 00054 If (n == 1 Or n == 2) 00055 Return(1); 00056 Else 00057 Return (recurFib(n - 1) + recurFib( n - 2)); 00058 } 00059 00060 Static Void para2() { 00061 /***************************************************************************** 00062 The recursive technique has crisper expressive power than the iterative approach because we see 00063 the inner structure of the sequence directly in the method of its construction. However, it is com- 00064 putationally much more expensive, especially for large n, because we must call the recurFib() 00065 method twice at each step, whereas iterFib() performs only one addition and minor data 00066 shuffling at each step. Here is an example where Knuth’s “goodness” criterion depends upon con- 00067 text. If efficiency is paramount, the iterative approach is preferred; recursion is preferable if 00068 expressive crispness is most important. 00069 00070 The Fibonacci sequence becomes relevant musically when we examine the ratios of subsequent 00071 terms: 00072 00073 1 2 3 5 8 13 00074 -,-,-,-,-,--, ... 00075 1 1 2 3 5 8 00076 00077 The corresponding sequence of quotients is 00078 00079 1, 200, 1.500, 1.670, 1.600, 1.625, 1.619, 1.617, 1.618, . . . 00080 00081 Thus we see that the ratio of adjacent Fibonacci numbers converges rapidly to the value of the 00082 golden mean, phi. The Greek letter phi is commonly used to stand for the golden 00083 mean. This number appears in a wide range of natural designs, including the arrangements of petals 00084 in flowers, seed clusters, and pine cones. Studied at least since Euclid wrote his Elements, the 00085 golden mean has appeared consciously and unconsciously as a central design element in countless 00086 musical works (see section 9.16.1). 00087 *****************************************************************************/ 00088 00089 For (Integer i = 1; i < 10; i++) 00090 Print(recurFib(i)); 00091 00092 /***************************************************************************** 00093 prints the same sequence. It is useful to watch recurFib() compute using 00094 a debugger because you can see how it recursively enters itself with 00095 ever-changing values. 00096 00097 *****************************************************************************/ 00098 }} 00099 00101 /* $Revision: 1.4 $ $Date: 2006/09/12 17:38:00 $ $Author: dgl $ $Name: $ $Id: _b0126_8cpp-source.html,v 1.4 2006/09/12 17:38:00 dgl Exp $ */ 00102 // The Musimat Tutorial © 2006 Gareth Loy 00103 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00104 // and published exclusively by The MIT Press. 00105 // This program is released WITHOUT ANY WARRANTY; without even the implied 00106 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00107 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00108 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00109 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00110 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00111 // The Musimat website: http://www.musimat.com/ 00112 // This program is released under the terms of the GNU General Public License 00113 // available here: http://www.gnu.org/licenses/gpl.txt 00114 00115
1.4.7