Subhayan Bhattacharya
Subhayan Bhattacharya

Reputation: 5715

Generating Fibonacci series in Python

I want to simple generate the fibonacci series in Python. But somehow i don't see the correct series. For example if i input 3 then the correct answer should come with the series : 1 1 2 3

Below is my code.Can someone please point out what is wrong with this :

def genfibonacci(no):
    if no <= 1:
        return no
    else:
        sum = genfibonacci(no - 1) + genfibonacci(no - 2)
    print (sum)
    return(sum)

number = int(input())
genfibonacci(number)

Thanks in advance.

Upvotes: 1

Views: 1520

Answers (5)

Sayantan Hutait
Sayantan Hutait

Reputation: 1

There are different methods you can use but I use the method of recursion because here you have to code less

    def fib(n):
    if n<=1:
        return n
    else:
        return fib(n-1)+fib(n-2)
n = int(input())
for i in range(n):
    print(fib(i),sep=' ',end=' ')

Upvotes: 0

Bhupathi KC
Bhupathi KC

Reputation: 105

Fibonacci using Recursive Function in Python:

def fibonacci(n, fib1=0, fib2=1):
  if n<=1:
    print(fib1)
    return 
  else:
    print(fib1)
    fib = fib1 + fib2
    fib1 = fib2
    fib2 = fib
    fibonacci(n - 1, fib1, fib2)

number = int(input())
fibonacci(number)

Upvotes: 0

ritesh sadhu
ritesh sadhu

Reputation: 1

Here the Simplest Fibonacci Series represnted:

#Fibonacci series in Short Code

#initilize the base list
ls2=[0,1]

#user input: How many wants to print
c=int(input('Enter required numbers:'))

#fibonacci Function to add last two elements of list
ls2.extend((ls2[i-1]+ls2[i-2]) for i in range(2,c))

#This will print recuired numbers of list
print(ls2[0:c])

If you Want Create Function Then:

#Fibonacci series in Short Code

#initilize the base list
ls2=[0,1]
#user input: How many wants to print
c=int(input('Enter required numbers:'))

#fibonacci Function to add last two elements of list
def fibonacci(c):    

    ls2.extend((ls2[i-1]+ls2[i-2]) for i in range(2,c))    
    #This will print required numbers of list
    print(ls2[0:c])

fibonacci(c)

Upvotes: 0

doctorlove
doctorlove

Reputation: 19232

Part of your problem is printing while you calculate (apart from if no <= 1) If we remove the print, and just show what you get as a result this will help:

def genfibonacci(no):
    if no <= 1:
        sum = no
    else:
        sum = genfibonacci(no-1) + genfibonacci(no-2)
    return sum

>>> [genfibonacci(i) for i in range(4)]
[0, 1, 1, 2]
>>> [genfibonacci(i) for i in range(5)]
[0, 1, 1, 2, 3]

This range starts at 0, so you can remove that if you want.

Since genfibonacci for say 4 will call 32 and 2, which in turn will call 2 and 1, the print statement you have will happen for the same number more than once. And not at all for the no <= 1.

Upvotes: 1

SumanKalyan
SumanKalyan

Reputation: 1761

There are so many ways to calculate fibonacci sesries in python..

Example 1: Using looping technique

def fib(n):
 a,b = 1,1
 for i in range(n-1):
  a,b = b,a+b
 return a
print fib(5)

Example 2: Using recursion

def fibR(n):
 if n==1 or n==2:
  return 1
 return fib(n-1)+fib(n-2)
print fibR(5)

Example 3: Using generators

a,b = 0,1
def fibI():
 global a,b
 while True:
  a,b = b, a+b
  yield a
f=fibI()
f.next()
f.next()
f.next()
f.next()
print f.next()

Example 4: Using memoization

def memoize(fn, arg):
 memo = {}
 if arg not in memo:
  memo[arg] = fn(arg)
  return memo[arg]

fib() as written in example 1.

fibm = memoize(fib,5)
print fibm

Example 5: Using memoization as decorator

class Memoize:
 def __init__(self, fn):
  self.fn = fn
  self.memo = {}
 def __call__(self, arg):
  if arg not in self.memo:
   self.memo[arg] = self.fn(arg)
   return self.memo[arg]

@Memoize
def fib(n):
 a,b = 1,1
 for i in range(n-1):
  a,b = b,a+b
 return a
print fib(5)

Upvotes: 1

Related Questions