fibonacci_series.py 563 B

12345678910111213141516171819202122232425262728293031
  1. def non_recur_fibo(n):
  2. curr = 0
  3. prev = 1
  4. lastprev = 0
  5. series = '0 1'
  6. for i in range(2, n):
  7. curr = prev + lastprev
  8. series += ' ' + str(curr)
  9. lastprev = prev
  10. prev = curr
  11. return series
  12. def recur_fibo(n):
  13. if n == 0:
  14. return 0
  15. elif n == 1:
  16. return 1
  17. else:
  18. return recur_fibo(n - 1) + recur_fibo(n - 2)
  19. n=int(input("Enter the terms: "))
  20. print('Number of fibonacci terms in a series', n)
  21. print('Series (non-recursive): ', non_recur_fibo(n))
  22. print('Series (recursive): ', end = '')
  23. for i in range(n):
  24. print(str(recur_fibo(i)) + ' ', end = '')