is the a python function for fibonacci numbers code example
Example 1: fibonacci sequence python
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)
def fib2(num):
a, b = 1, 1
for _ in range(num - 1):
a, b = b, a + b
return a
print(fib(6))
print(fib2(6))
Example 2: fibonacci sequence python
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='')
amount_of_digits = amount_of_digits + 1
if amount_of_digits == digits:
print('\n')
print('Loop done.')
break
fibonacci(10)