Advertisement
Acer1968

Comparation between two method for fibo counting

Aug 9th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. # Function for nth fibonacci number - Space Optimisataion
  2. # Taking 1st two fibonacci numbers as 0 and 1
  3. # This code is contributed by Saket Modi
  4.  
  5. def fibonacci(n):
  6.     a = 0
  7.     b = 1
  8.     if n < 0:
  9.         print("Incorrect input")
  10.     elif n == 0:
  11.         return a
  12.     elif n == 1:
  13.         return b
  14.     else:
  15.         for i in range(2,n):
  16.             c = a + b
  17.             a = b
  18.             b = c
  19.         return b
  20.  
  21.  
  22. # Function for nth fibonacci number via GOLDEN RATIO
  23. # Taking 1st fibonacci number 1
  24. # This code is contributed by Damiano de Stefano
  25.  
  26. def fibo(n):
  27.     phi=(1+5**0.5)/2
  28.     return(int((phi**n-(1-phi)**n)/5**0.5))
  29.  
  30.  
  31. # Comparation of these two functions
  32. # last position with correct result is for n=71 !!!!!
  33.  
  34. print ("n  | "+"Correct result".ljust(22)+" | "+"Damiano´s wrong result".ljust(22)+" | "+"Difference")
  35. for n in range (70,80):
  36.     a=fibonacci(n+1)
  37.     b=fibo(n)
  38.     print(str(n).ljust(4),str(a).ljust(25),str(b).ljust(25),str(a-b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement