gpt4 book ai didi

python - 在 Python 中检查属于斐波那契数列的输入

转载 作者:太空狗 更新时间:2023-10-30 00:27:01 25 4
gpt4 key购买 nike

我想问一下如何检索出斐波纳奇列表的列表,然后检查用户输入的值是否在斐波纳奇列表中。

    a , b = 1, 1
while num <= sys.maxint:
fibo == a , b = b, a+b
if num == (b +a+b):
print "It is a Fibonacci number"
break
else:
print "It is not a Fibonacci number"
break

谢谢!

最佳答案

使用 more sophisticated Fibonacci number test ,你可以使用

def is_fibonacci(n):
phi = 0.5 + 0.5 * math.sqrt(5.0)
a = phi * n
return n == 0 or abs(round(a) - a) < 1.0 / n

(这可能是确定一个数字是否为斐波那契数的最有效方法,而且它很可能不是您作业的预期解决方案。我只是包含这个答案以供将来引用。)

关于python - 在 Python 中检查属于斐波那契数列的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7472128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com