Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function for nth fibonacci number - Space Optimisataion
- # Taking 1st two fibonacci numbers as 0 and 1
- # This code is contributed by Saket Modi
- def fibonacci(n):
- a = 0
- b = 1
- if n < 0:
- print("Incorrect input")
- elif n == 0:
- return a
- elif n == 1:
- return b
- else:
- for i in range(2,n):
- c = a + b
- a = b
- b = c
- return b
- # Function for nth fibonacci number via GOLDEN RATIO
- # Taking 1st fibonacci number 1
- # This code is contributed by Damiano de Stefano
- def fibo(n):
- phi=(1+5**0.5)/2
- return(int((phi**n-(1-phi)**n)/5**0.5))
- # Comparation of these two functions
- # last position with correct result is for n=71 !!!!!
- print ("n | "+"Correct result".ljust(22)+" | "+"Damiano´s wrong result".ljust(22)+" | "+"Difference")
- for n in range (70,80):
- a=fibonacci(n+1)
- b=fibo(n)
- 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