Thursday, March 19, 2015

Fibonacci Sequence in Python

Hello, today i'll gonna show you a little example of Fibonacci sequence in python, using recursivity.
In math, Fibonacci sequence is the numbers in the following sequence:
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\;
By definition, the first two numbers in the Fibonacci sequence are either 0 and 1, and each subsequent number is the sum of the previous two.
Below is the code (only 7 lines), using recursion to find the number in the Fibonacci sequence, according to the index past by parameter. Ex.: Fibonacci(11) = 89.
def Fibonacci(n):
    if n <= 1:
        return n
    else:
        return Fibonacci(n-1) + Fibonacci(n-2)
print('Fibonacci = ' + str(Fibonacci(10)) )

If you want to know more about the Fibonacci sequence, visit this link.

Any questions just comment

No comments:

Post a Comment