Functions

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

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0126)
Integer iterFib (Integer n)
Static Void para1 ()
Integer recurFib (Integer n)
Static Void para2 ()

Function Documentation

Integer iterFib ( Integer  n )

Definition at line 27 of file B0126.cpp.

                           {
        Integer fn1 = 1;
        Integer fn2 = 1;
        Integer result = 1;
        
        For (Integer i = 2; i < n; i = i + 1) {
                result = fn1 + fn2;
                fn2 = fn1;
                fn1 = result;
        }
        Return (result);
}
MusimatTutorialSection ( B0126   )

Definition at line 2 of file B0126.cpp.

References para1(), and para2().

                              {
        Print("*** B.1.26 Fibonacci Numbers ***");
        /*****************************************************************************
         
         B.1.26 Fibonacci Numbers
         
         In the sequence
         
         1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 377, 610, 987, 1597, 2584, . . .
         
         each subsequent term is the sum of its two immediately preceding values. For example, 8 = 5 + 3. 
         This series, invented by Leonardo Pisano Fibonacci (1170-1250), is the solution to a problem he 
         posed in his book "Liber Abaci": "A certain man put a pair of rabbits in a place surrounded on 
         all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is 
         supposed that every month each pair begets a new pair which from the second month on becomes 
         productive?"
         
         An iterative method of computing the Fibonacci sequence is shown below
         in function iterFib().
         *****************************************************************************/
        
        para1(); // Step into this function to continue the tutorial
        para2(); // Step into this function to continue the tutorial
}
Static Void para1 (  )

Definition at line 40 of file B0126.cpp.

References iterFib().

                    {
        
        /*****************************************************************************
         Executing the following For loop,
         *****************************************************************************/ 
        
        For ( Integer i = 1; i < 10; i = i + 1 )
                Print(iterFib(i));
}
Static Void para2 (  )

Definition at line 62 of file B0126.cpp.

References recurFib().

                    {
        
        /*****************************************************************************
         The recursive technique has crisper expressive power than the iterative approach because we see 
         the inner structure of the sequence directly in the method of its construction. However, it is com-
         putationally much more expensive, especially for large n, because we must call the recurFib() 
         method twice at each step, whereas iterFib() performs only one addition and minor data 
         shuffling at each step. Here is an example where Knuth's "goodness" criterion depends upon con-
         text. If efficiency is paramount, the iterative approach is preferred; recursion is preferable if 
         expressive crispness is most important.
         
         The Fibonacci sequence becomes relevant musically when we examine the ratios of subsequent 
         terms:
         
         1 2 3 5 8 13
         -,-,-,-,-,--, ...
         1 1 2 3 5  8
         
         The corresponding sequence of quotients is
         
         1, 200, 1.500, 1.670, 1.600, 1.625, 1.619, 1.617, 1.618, . . .
         
         Thus we see that the ratio of adjacent Fibonacci numbers converges rapidly to the value of the 
         golden mean, phi. The Greek letter phi is commonly used to stand for the golden 
         mean. This number appears in a wide range of natural designs, including the arrangements of petals 
         in flowers, seed clusters, and pine cones. Studied at least since Euclid wrote his Elements, the 
         golden mean has appeared consciously and unconsciously as a central design element in countless 
         musical works (see section 9.16.1).
         *****************************************************************************/
        
        For (Integer i = 1; i < 10; i++)
                Print(recurFib(i));
        
        /*****************************************************************************
         prints the same sequence.  It is useful to watch recurFib() compute using
         a debugger because you can see how it recursively enters itself with
         ever-changing values.
         
         *****************************************************************************/
}
Integer recurFib ( Integer  n )

Definition at line 55 of file B0126.cpp.

                            {
        If (n == 1 Or n == 2)
                Return(1);
        Else
                Return (recurFib(n - 1) + recurFib( n - 2));
}