xxxxxxxxxx
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib = [0, 1]
for i in range(2, n):
fib.append(fib[-1] + fib[-2])
return fib
xxxxxxxxxx
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
xxxxxxxxxx
# 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
xxxxxxxxxx
# Call fib(range) for loop iteration
def fib(rng):
a, b, c = 0, 1, 0
for i in range(rng):
c = a + b; a = b; b = c
print(a)
fib(10)
xxxxxxxxxx
num = 1
num1 = 0
num2 = 1
import time
for i in range(0, 10):
print(num)
num = num1 + num2
num1 = num2
num2 = num
time.sleep(1)
xxxxxxxxxx
def fibNum(n):
f = [0] * n
f[0] = 0
f[1] = 1
for x in range(2, n):
f[x] = f[x-1]+f[x-2]
return (f[n-1])
fibonacci sequence python
xxxxxxxxxx
import math
fibonacci = []
# Fills the fibonacci sequence with the first 500 numbers
for i in range(500):
fibonacci.append(round(((1 + math.sqrt(5)) / 2) ** i / math.sqrt(5)))
print(fibonacci)
fibonacci sequence python
xxxxxxxxxx
# ChatGPT made this one
# Function to generate the Fibonacci sequence up to n terms
def fibonacci(n):
# Initialize the first two terms
fib = [0, 1]
# Generate the next terms using the previous two
for i in range(2, n):
next_fib = fib[i-1] + fib[i-2]
fib.append(next_fib)
return fib
# Generate the first 10 terms of the Fibonacci sequence
fibonacci_sequence = fibonacci(10)
print(fibonacci_sequence)
xxxxxxxxxx
def fib(n: int) -> int:
if n in {0,1}:
return n
a,b = 0,1
for i in range(n-1):
a,b = b,a+b
return b
xxxxxxxxxx
Number = int(input("How many terms? "))
# first two terms
First_Value, Second_Value = 0, 1
i = 0
if Number <= 0:
print("Please enter a positive integer")
elif Number == 1:
print("Fibonacci sequence upto",Number,":")
print(First_Value)
else:
print("Fibonacci sequence:")
while i < Number:
print(First_Value)
Next = First_Value + Second_Value
# update values
First_Value = Second_Value
Second_Value = Next
i += 1