Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Fibonnacci数小于设定值_Python_Python 3.x_Fibonacci - Fatal编程技术网

Python Fibonnacci数小于设定值

Python Fibonnacci数小于设定值,python,python-3.x,fibonacci,Python,Python 3.x,Fibonacci,为什么这段代码不生成小于设定值的斐波那契数 from math import sqrt def Fib(n): return round(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))) def Fiblessthan(m): total = [0] count = 1 while max(total) < m: total.append(Fib(count)) count =

为什么这段代码不生成小于设定值的斐波那契数

from math import sqrt
def Fib(n):
    return round(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5)))

def Fiblessthan(m):
    total = [0]
    count = 1
    while max(total) < m:
        total.append(Fib(count))
        count = count + 1
从数学导入sqrt
def纤维(n):
回程(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5)))
def纤维含量(m):
总计=[0]
计数=1
当最大值(总值)
例如,我不能打印所有小于4000000的斐波那契数。这是正确的方法吗。

来自math import sqrt
from math import sqrt
def Fib(n):
    return round(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5)))

def Fiblessthan(m):
    total = [0]
    count = 1
    while total[-1] < m:
        total.append(Fib(count))
        count = count + 1
    return total
def纤维(n): 回程(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))) def纤维含量(m): 总计=[0] 计数=1 当总量[-1]

您必须检查total的长度是否小于m。

它的作用是什么?它不应该是
max(total)
,而max(total)>m:
在开始时不是真的。你的fib函数不是计算fib值的最佳方法!Wikunia什么是最好的fib函数。警告:由于精度有限,您的函数在n=71时停止工作。当答案为308061521170129时,您的
Fib
返回308061521170130.0。然后返回输入长度的斐波那契数列表。我想要一个函数,它能产生一定数量的斐波那契数value@phighter根据需要对其进行修改:)