Fibonacci Sequence Music and Recursion Sucks

The notes shown are based on the Fibonacci Sequence--a mathematical sequence that begins with the numbers 1, 1. All of the other numbers in the sequence are produced by adding the previous two numbers.

So, 1 + 1 = 2. (Now the sequence is 1, 1, 2.) Then 1 + 2 = 3. (Now the sequence is 1, 1, 2, 3.) Then 2 + 3 = 5. (Now the sequence is 1, 1, 2, 3, 5.) And on and on.

The way I wrote this piece was to just assign the C below middle-C to 1. The next whole note up (using only the white keys on the piano) is D, which is assigned to 2, and so on. Just click above in the trinket to listen to the notes if you want. Could you write the next note in the sequence?

  1. What is the next number in the sequence?

  2. Can you figure out the name of the next note in the sequence?

I'd like to compose a piece of music based on the Fibonacci numbers, but I can see already that the numbers "grow" too quickly. Even if we start much lower on the keyboard, we'll run out of room too fast to compose anything interesting.


Modular Arithmetic

First, let me start by being able to generate any Fibonacci number that I want, rather than having to look up the numbers in a list. The Fibonacci sequence is also a function, which can be defined recursively as follows: \[\small\mathtt{ F(n) =} \left\{\begin{array}{ll} \small\mathtt{1,} & \quad \small\mathtt{n = 1} \\ \small\mathtt{1,} & \quad \small\mathtt{n = 2} \\ \small\mathtt{F(n-1) + F(n-2),} & \quad \small\mathtt{n > 2} \end{array} \right. \]

Notice that this is a piecewise function with 3 pieces. The variable \(\small\mathtt{n}\) goes from 1 to infinity and just counts the numbers in the list (1st Fibonacci number, 2nd Fibonacci number, and so on). The first piece (the first line) of the piecewise function says that the 1st Fibonacci number is 1. The second piece says that the 2nd Fibonacci number is also 1. And the third piece tells us that the \(\small\mathtt{n}\)th Fibonacci number (3rd or greater) is the sum of the two previous Fibonacci numbers: \(\small\mathtt{F(n-1) + F(n-2)}\).

This is awful. Recursion is awful. The following formula is just better, although it's a little more difficult to understand. But we don't have to understand how everything is put together before we use it. \[\small\mathtt{ F(n) = \frac{(1 + \sqrt{5})^n - (1 - \sqrt{5})^n}{(2^n)(\sqrt{5})}} \]

We can then write a program to generate these numbers for us:

This program returns the first 20 Fibonacci numbers. And if you can read this code, or go read about it, you can change it to get more numbers. But now I'm going to restrict those numbers to a certain range, which is where modular arithmetic comes in. I'll take each of those numbers and divide them by, say, 11. The result that I want will be a list of the remainders of each division.

Here's what I get for the first 20 Fibonacci numbers (we can say that these are the first 20 Fibonacci numbers, "mod 11"). This program at the right actually has a creepy issue with it. Can you identify it?






  1. Which Fibonacci numbers show incorrect remainders in the program?

  2. What remainder should be displayed for each of the numbers in Question 3?




Play That Fibonacci Music

The reason we get some buggy numbers is that operating with floating point numbers is tricky when what we want are integer remainders. I've improved the program's results a bit at the right by first rounding the numbers returned by the function and then turning these numbers into integers. I'll look into further improvements for the next part of this post, but it looks like we get correct values for at least the first 50 Fibonacci numbers and their corresponding "mod 11" values.

So, now I can use the same methodology I started writing music with to produce something of a little greater length. Of course, this makes the pattern you might have noticed in the remainder lists unmistakeable! Do you think we'd still get this pattern if we used a different "mod"?