Answers for "fibonacci no python"

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
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
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
0

fibonacci sequence python

# This program also assumes the fibonacci sequence starts at 1 
# This code will print as many digits as the user wishes
# Instead of printing a specific number in the sequence 
def fibonacci(digits):

    digit1, digit2 = 1, 1
    amount_of_digits = 0 
    print(str(digit1) + ", " + str(digit2) + ', ', end='')

    while amount_of_digits != digits:
        digit1, digit2 = digit2, digit1 + digit2 
        print(str(digit2) + ", ", end='') # we want to print on the same line
        amount_of_digits = amount_of_digits + 1 

        if amount_of_digits == digits:
            print('\n') 
            print('Loop done.')
            break

fibonacci(10) # this will print the first 10 digits in the sequence
Posted by: Guest on May-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language