Answers for "python fibonacci function"

10

fibonacci sequence python

# 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
2

fibonacci sequence python

# 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)


print(fib(6))  # 8
Posted by: Guest on April-10-2020
0

fibonacci sequence python

def fib(n):
    a = 0
    b = 1

    print(a)    
    print(b)

    for i in range(2, n):
        print(a+b)
        a, b = b, a + b

fib(7) #first seven nubers of Fibonacci sequence
Posted by: Guest on February-07-2021
1

code fibonacci python

# Program to display the Fibonacci sequence forever

def fibonacci(i,j):
  print(i;fibonacci(j,i+j))
fibonacci(1,1)
Posted by: Guest on November-09-2020
0

fibonacci python

# This function computes and returns fibonacci 
# sequence number using Dynamic Programming
def fib(num, memo{}):
	if num in memo:
	    return memo[num]
	if num <= 2:
      return 1
    
   	memo[n] = fib(num-1, memo) + fib(num-2, memo)
    return memo[n]
	
  	"""
	  	params:
	    	int num: index of fibonacci sequence
	        dict memo: This object is needed to perform dynamic programming. Leave it empty
        return:
        	int output.
        complexity:
        	time: O(n)
            memory: O(n)
  	"""
Posted by: Guest on October-12-2021

Code answers related to "python fibonacci function"

Python Answers by Framework

Browse Popular Code Answers by Language