Ejemplo 1: python de secuencia de fibonacci
# 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
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: 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 4: serie de fibonacci usando for loop en python
#Learnprogramo
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
Ejemplo 5: serie de fibonacci para loop python
# # this is the fibonacci series by KV for for loop :)
n = int(input(":"))
for i in range(1,n+1):
print(i, end = "")
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)