Answers for "python fibonacci"

1

python fibonacci generator

def fib(num):
    a = 0
    b = 1
    for i in range(num):
        yield a
        temp = a
        a = b
        b = temp + b


for x in fib(100):
    print(x)


def fib2(num): # Creates fib numbers in a list
    a = 0
    b = 1
    result = []
    for i in range(num):
        result.append(a)
        temp = a
        a = b
        b = temp + b
    return result


print(fib2(100))
Posted by: Guest on July-04-2020
5

how to create fibonacci sequence in python

#Python program to generate Fibonacci series until 'n' value
n = int(input("Enter the value of 'n': "))
a = 0
b = 1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
  print(sum, end = " ")
  count += 1
  a = b
  b = sum
  sum = a + b
Posted by: Guest on May-21-2020
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

python fibonacci

def Fibonacci( pos ):
        #check for the terminating condition
        if pos <= 1 :
                #Return the value for position 1, here it is 0
                return 0
        if pos == 2:
                #return the value for position 2, here it is 1
                return 1
 
        #perform some operation with the arguments
        #Calculate the (n-1)th number by calling the function itself
        n_1 = Fibonacci( pos-1 )
 
        #calculation  the (n-2)th number by calling the function itself again
        n_2 = Fibonacci( pos-2 )
 
        #calculate the fibo number
        n = n_1 + n_2
 
        #return the fibo number
        return n
 
#Here we asking the function to calculate 5th Fibonacci
nth_fibo = Fibonacci( 5 ) 
 
print (nth_fibo)
Posted by: Guest on April-19-2021
1

fibonacci python

# Implement the fibonacci sequence
	# (0, 1, 1, 2, 3, 5, 8, 13, etc...)
	def fib(n):
		if n== 0 or n== 1:
			return n
		return fib (n- 1) + fib (n- 2)
Posted by: Guest on May-03-2021
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

Python Answers by Framework

Browse Popular Code Answers by Language