Advertisement
yukcheong

Fibonacci number

Nov 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. from math import *
  2. def fibonacci_term(n):
  3.     if n==1:
  4.         return 0
  5.     elif n==2:
  6.         return 1
  7.     else:
  8.         return fibonacci_term(n-1)+fibonacci_term(n-2)
  9.  
  10. def previousfibonacci(n):  
  11.     if n == 0:
  12.         return float(1)
  13.     else:
  14.         a = n/((1 + sqrt(5))/2.0)
  15.         return round(a)  
  16.  
  17.  
  18. def fibonacci(start_term,numofterm):
  19.     if start_term<0:
  20.         print("Incorrect input")
  21.     else:
  22.         print float(fibonacci_term(start_term))
  23.         fiblist=[]
  24.         n = numofterm
  25.         fiblist.append(float(fibonacci_term(start_term)))
  26.         temp = fibonacci_term(start_term)
  27.         while n > 1 :
  28.             temp2 = temp + previousfibonacci(temp)
  29.             print temp2
  30.             fiblist.append(temp2)
  31.             temp = temp2
  32.             n -= 1
  33.         else:
  34.             print fiblist
  35.  
  36. fibonacci(2,20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement