Fibonacci Sequence

A Fibonacci sequence is a series of numbers in which each number (Fibonacci number) is the sum of the two preceding numbers.

 

Simple Fibonacci sequence

Generating the sequence:

Let's examine the simpliest Fibonacci series and how the sequence is generated.

 

Start with 0 and 1 as the first two numbers.

First number = 0

Second number = 1

Add the two numbers 0 + 1 (to get the third number) = 1

 

We now have:

First number = 0

Second number = 1

Third number = 1

Then add the next two numbers (number two and number 3) 1 + 1 (to get the fourth number) = 2

 

We now have:

First number = 0

Second number = 1

Third number = 1

Fourth number = 2

Then add the next two numbers (number 3 and number 4) 1 + 2 (to get the fifth number) = 3

 

We now have:

First number = 0

Second number = 1

Third number = 1

Fourth number = 2

Fifth number = 3

Then add the next two numbers (number 4 and number 5) 2 + 3 (to get the sixth number) = 5

 

We now have:

First number = 0

Second number = 1

Third number = 1

Fourth number = 2

Fifth number = 3

Sixth number = 5

The process can be repeated for the number of elements desired.

 

Here is the first ten elements in Fibonacci's sequence:

First number = 0

Second number = 1

Third number = 1

Fourth number = 2

Fifth number = 3

Sixth number = 5

Seventh number = 8

Eight number = 13

Ninth number = 21

Tenth number = 34

Crazy how they are getting larger!

Wonder what a graph of the sequence would look like?

 

More extension questions!

What are the next 10 numers?

How many numbers will it take before the sequence reashers or passes 1 000?

10 000?

100 000?

1 000 000?

If the number of numbers it takes to reach a higher number decreases, then will the number sequence end?

 

That's all ...

 

Enjoy!

 

Python code to generate series less than 100.

Copy and paste the code below here into the IDLE code editor and run the program. You can change the number in fib2(100) for sequences less than whatever number you put between the parentheses.

 


def fib2(n):  # return Fibonacci series up to n
    """Return a list containing the Fibonacci series up to n."""
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)    # see below
        a, b = b, a+b
    return result

f100 = fib2(100)    # call it
print (f100)                # write the result

PHP program to generate a Fibonacci sequence

 

<?php
//PHP code to calculate Fibonacci sequence;
$num = 0;
$n1 = 0;
$n2 = 1;
echo "<h2>Fibonnacci Sequence " . $x . "<h2>";
echo "<br>";
echo $y;
echo "<h3>Fibonacci series for the first 12 numbers!<h3>";
echo "\n";
echo $n1 . " " . $n2 . " ";
while ($num<10){
$n3=$n2+$n1;
echo($n3.' ');
$n1=$n2;
$n2=$n3;
$num=$num+1;
}
?>

 

Link to a PHP program that will run this program and print the results on an html page.

 

 

 

 

Top

Dr. Robert Sweetland's notes
[Home: homeofbob.com & thehob.net ]