- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/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 26 of file B0126.cpp.

    00026                            {
    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 }

MusimatTutorialSection | ( | B0126 |  | ) |

Definition at line 2 of file B0126.cpp.

References para1(), and para2().

    00002                               {
    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 }

Static Void para1 | ( |  | ) |

Definition at line 39 of file B0126.cpp.

References iterFib().

    00039                     {
    00040 /*****************************************************************************
    00041  Executing the following For loop,
    00042  *****************************************************************************/
    00043
    00044 For ( Integer i = 1; i < 10; i = i + 1 )
    00045                 Print(iterFib(i));
    00046 }

Static Void para2 | ( |  | ) |

Definition at line 60 of file B0126.cpp.

References recurFib().

    00060                     {
    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 }}

Integer recurFib | ( | Integer | n | ) |

Definition at line 53 of file B0126.cpp.

    00053                             {
    00054         If (n == 1 Or n == 2)
    00055                 Return(1);
    00056         Else
    00057                 Return (recurFib(n - 1) + recurFib( n - 2));
    00058 }