Ejemplo 1: generador de python fibonacci
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))
Ejemplo 2: cómo crear una secuencia de fibonacci en 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
Ejemplo 3: serie de fibonacci usando recursividad en python
# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Ejemplo 4: python de secuencia de fibonacci
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)
Ejemplo 5: serie de fibonacci en python
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Ejemplo 6: serie de fibonacci en python
~~ This is the best Fibonacci sequence generator as you have all the option that
till what number should this go on for ~~
a = 0
b = 1
f = 1
n = int(input("Till what number would you like to see the fibonacci sequence: "))
while b <= n:
f = a+b
a = b
b = f
print(a)
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)