Programming Python Fibonacci Numbers


The Fibonacci sequence is named after Italian mathematician Leonardo of Pisa, known as Fibonacci. His 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been described earlier as Virahanka numbers in Indian mathematics.

<img src="python.png" alt="python">


By modern convention, the sequence begins either with F0 = 0 or with F1 = 1.
The next number is found by adding up the two numbers before it.
The 2 is found by adding the two numbers before it (1 + 1), similarly, the 3 is found by adding the two numbers before it (1 + 2), next the 5 is (2+3), and so on.

Example: the next number in the sequence above is 21 + 34 = 55

It is that simple, and here is a longer list:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, ...

<img src="fibonacci_python.png" alt="fibonacci_python">


Here is the source code for Fibonacci sequence.

#Python
#Program fibonacci Loki Lang
print 'Masukkan suatu nilai:'
num = int(input())
if num < 0:
    num *= -1
    a, b = 0, -1
    for i in range(num):
        a, b = b, a - b
        print 'Bilangan fibonacci', (i + 1) * -1, 'ialah', a
    num *= -1
else:
    a, b = 0, 1
    for i in range(num):
        a, b = b, a + b
        print 'Bilangan fibonacci', i + 1, 'ialah', a
print 'Nilai fibonacci', num ,'ialah', a