Fibonacci Sequence in Python

What is Fibonacci Sequence?

Depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

Fibonacci Sequence:

0,1,1,2,3,5,8,13,21,34,55,89,144,233, …
1,1,2,3,5,8,13,21,34,55,89,144,233, …

Fibonacci Sequence Formula:

Fibonacci Sequence in Python

Fibonacci Sequence in Python Examples:

Fibonacci Sequence in Python using looping technique:

def fibonacci(n):
x,y = 0,1
for i in range(n-1):
x,y = y,x+y
return x
print fibonacci(10)

Fibonacci Sequence in Python using recursion:

def fibonacci(n):
if n==1 or n==2:
return 1
return fibonacci(n-1)+fibonacci(n-2)
print fibonacci(10)

Related Posts

Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools