嵌套循环斐波那契数列
Nested Loops Fibonacci Sequence
我正在尝试编写一个程序,要求用户输入一个数字。然后我需要验证它是否在 fib 序列中
代码:
# asking the user to input number
number = int(input("Enter a number: "))
# creating empty list to be filled
sequence = [0, 1]
x = -2
y = -1
# append items to list
for i in range(number):
x+=2
y+=2
# this code will be executed 'length' times
sequence.append(x+y)
# This should be somewhere in the loop:
if number in sequence:
print("The number is in the Fibonacci sequence")
else:
print("The number is not in the Fibonacci sequence")
预期输出:
斐波那契数列 = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Enter a number: 5
>>> The number is in the Fibonacci sequence
序列没有位置 x 或 y。您附加到序列的行应显示为 sequence.append(x+y)
可能是我想的太简单了,但是根据你的问题,好像是给你提供了一个斐波那契数列。如果是这种情况,您可以只使用 in
,即
x = 5 # this will be user's input
fib = [0,1,1,2,3,5,8,13,21,34]
if x in fib:
print('Number is in sequence')
else:
print('Number is not in sequence')
如果您必须生成自己的斐波那契数列,那么您可能需要考虑使用递归。
您需要进行一些迭代(或递归)才能找到斐波那契数列。这是使用 while 循环执行此操作的一种方法:
number = int(input("Enter a number: "))
sequence = [0, 1]
i= 0
while True:
new_item = sequence[i] + sequence[i+1]
if new_item == number or number in [0,1]:
print("The number is in the Fibonacci sequence")
break
elif new_item > number:
print("The number is not in the Fibonacci sequence")
break
sequence.append(new_item)
i+=1
请注意,您将迭代直到斐波那契数列中的新项目大于或等于用户输入的数字。
或者你可以只计算斐波那契数
def fibo(n):
p = (1+5**.5)/2
q = (1-5**.5)/2
return int(1/5**.5*(p**n-q**n))
def fiba(num):
count = 0
while fibo(count) <= num:
if fibo(count) == num:
return "YES!"
count += 1
return "NO!"
一种方法是将 fibonacci
分解为一个生成器,然后迭代该生成器直到 >= number
,例如:
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
In []
number = 5 # int(input("Enter a number: "))
for f in fib():
if f >= number:
break
if number == f:
print("The number is in the Fibonacci sequence")
else:
print("The number is not in the Fibonacci sequence")
Out[]:
The number is in the Fibonacci sequence
你可以用itertools.takewhile
代替for循环,例如:
import itertools as it
from collections import deque
f = deque(it.takewhile(lambda n: n <= number, fib()), maxlen=1).pop()
...
我正在尝试编写一个程序,要求用户输入一个数字。然后我需要验证它是否在 fib 序列中
代码:
# asking the user to input number
number = int(input("Enter a number: "))
# creating empty list to be filled
sequence = [0, 1]
x = -2
y = -1
# append items to list
for i in range(number):
x+=2
y+=2
# this code will be executed 'length' times
sequence.append(x+y)
# This should be somewhere in the loop:
if number in sequence:
print("The number is in the Fibonacci sequence")
else:
print("The number is not in the Fibonacci sequence")
预期输出:
斐波那契数列 = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Enter a number: 5
>>> The number is in the Fibonacci sequence
序列没有位置 x 或 y。您附加到序列的行应显示为 sequence.append(x+y)
可能是我想的太简单了,但是根据你的问题,好像是给你提供了一个斐波那契数列。如果是这种情况,您可以只使用 in
,即
x = 5 # this will be user's input
fib = [0,1,1,2,3,5,8,13,21,34]
if x in fib:
print('Number is in sequence')
else:
print('Number is not in sequence')
如果您必须生成自己的斐波那契数列,那么您可能需要考虑使用递归。
您需要进行一些迭代(或递归)才能找到斐波那契数列。这是使用 while 循环执行此操作的一种方法:
number = int(input("Enter a number: "))
sequence = [0, 1]
i= 0
while True:
new_item = sequence[i] + sequence[i+1]
if new_item == number or number in [0,1]:
print("The number is in the Fibonacci sequence")
break
elif new_item > number:
print("The number is not in the Fibonacci sequence")
break
sequence.append(new_item)
i+=1
请注意,您将迭代直到斐波那契数列中的新项目大于或等于用户输入的数字。
或者你可以只计算斐波那契数
def fibo(n):
p = (1+5**.5)/2
q = (1-5**.5)/2
return int(1/5**.5*(p**n-q**n))
def fiba(num):
count = 0
while fibo(count) <= num:
if fibo(count) == num:
return "YES!"
count += 1
return "NO!"
一种方法是将 fibonacci
分解为一个生成器,然后迭代该生成器直到 >= number
,例如:
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
In []
number = 5 # int(input("Enter a number: "))
for f in fib():
if f >= number:
break
if number == f:
print("The number is in the Fibonacci sequence")
else:
print("The number is not in the Fibonacci sequence")
Out[]:
The number is in the Fibonacci sequence
你可以用itertools.takewhile
代替for循环,例如:
import itertools as it
from collections import deque
f = deque(it.takewhile(lambda n: n <= number, fib()), maxlen=1).pop()
...