Answers for "what does fibonacci mean"

10

fibonacci

# WARNING: this program assumes the
# fibonacci sequence starts at 1
def fib(num):
	"""return the number at index `num` in the fibonacci sequence"""
    if num <= 2:
        return 1
    return fib(num - 1) + fib(num - 2)

# method 2: use `for` loop
def fib2(num):
	a, b = 1, 1
	for _ in range(num - 1):
		a, b = b, a + b
	return a


print(fib(6))  # 8
print(fib2(6))  # same result, but much faster
Posted by: Guest on February-24-2020
0

fibonacci

fib(5)   
                     /                  
               fib(4)                fib(3)   
             /                      /     
         fib(3)      fib(2)         fib(2)    fib(1)
        /             /           /      
  fib(2)   fib(1)  fib(1) fib(0) fib(1) fib(0)
  /    
fib(1) fib(0)
Posted by: Guest on June-11-2021

Python Answers by Framework

Browse Popular Code Answers by Language