Python 中的偶数斐波那契数列的这段代码有什么问题?
What is wrong with this code for an even-number fibonacci sequence in Python?
def fibonacci(n):
first = 0
second = 1
count = 0
while count <= n:
if (second % 2 == 0):
first, second = second, first + second
count += 1
return first, second
print (fibonacci(4000000))
有人可以解释一下这段代码有什么问题吗?在空闲状态下,页面重新启动,但 returns 没有应答。顺便说一下,我是一名初级程序员,刚刚完成 CodeAcademy 课程。
second % 2
从 1 开始,这不是奇数。因此,您的 while
循环不会 运行 体内的任何东西,因此循环 运行 永远存在。
您可能希望始终计算下一个斐波那契数,但仅在 second
为偶数时才增加计数。
你的问题是你的 count 变量在 if 语句中。您已经创建了一个无限循环。将它移到 if 语句之外:
if(second % 2 == 0):
first, second = second, first + second
count +=1
此外,您还必须添加更多代码才能使其正常工作。
由于这是 Problem 2 of Project Euler,您计算的值有误。它要求所有偶数斐波那契数的总和达到 value 四百万,而不是四百万分之一的值。那会太大了。
由于我们要不断生成值,因此我们将使用生成器并将其与 itertools.takewhile()
. Then we'll filter()
it down to the even values, and find the sum()
结合使用。
import itertools
def fibonacci_gen():
first = 0
second = 1
while 1:
first, second = second, first + second
yield second
>>> a = fibonacci_gen()
>>> sum(filter(lambda y: not y%2, itertools.takewhile(lambda x: x<=4e6, a)))
4613732
对于不使用这些功能的解决方案:
def fibonacci_4m():
result = 0
first = 0
second = 1
while second <= 4e6:
first, second = second, first + second
if not second%2:
result += second
return result
>>> fibonacci_4m()
4613732
在您的 while 循环中,当 if 语句未能执行其条件代码时,不会发生任何变化。请尝试以下操作:
def main():
for index in range(1, 41):
print('even_fibonacci({}) = {}'.format(index, even_fibonacci(index)))
def even_fibonacci(index):
for number in all_fibonacci():
if not number & 1:
index -= 1
if not index:
return number
def all_fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
if __name__ == '__main__':
main()
def fibonacci(n):
first = 0
second = 1
count = 0
while count <= n:
if (second % 2 == 0):
first, second = second, first + second
count += 1
return first, second
print (fibonacci(4000000))
有人可以解释一下这段代码有什么问题吗?在空闲状态下,页面重新启动,但 returns 没有应答。顺便说一下,我是一名初级程序员,刚刚完成 CodeAcademy 课程。
second % 2
从 1 开始,这不是奇数。因此,您的 while
循环不会 运行 体内的任何东西,因此循环 运行 永远存在。
您可能希望始终计算下一个斐波那契数,但仅在 second
为偶数时才增加计数。
你的问题是你的 count 变量在 if 语句中。您已经创建了一个无限循环。将它移到 if 语句之外:
if(second % 2 == 0):
first, second = second, first + second
count +=1
此外,您还必须添加更多代码才能使其正常工作。
由于这是 Problem 2 of Project Euler,您计算的值有误。它要求所有偶数斐波那契数的总和达到 value 四百万,而不是四百万分之一的值。那会太大了。
由于我们要不断生成值,因此我们将使用生成器并将其与 itertools.takewhile()
. Then we'll filter()
it down to the even values, and find the sum()
结合使用。
import itertools
def fibonacci_gen():
first = 0
second = 1
while 1:
first, second = second, first + second
yield second
>>> a = fibonacci_gen()
>>> sum(filter(lambda y: not y%2, itertools.takewhile(lambda x: x<=4e6, a)))
4613732
对于不使用这些功能的解决方案:
def fibonacci_4m():
result = 0
first = 0
second = 1
while second <= 4e6:
first, second = second, first + second
if not second%2:
result += second
return result
>>> fibonacci_4m()
4613732
在您的 while 循环中,当 if 语句未能执行其条件代码时,不会发生任何变化。请尝试以下操作:
def main():
for index in range(1, 41):
print('even_fibonacci({}) = {}'.format(index, even_fibonacci(index)))
def even_fibonacci(index):
for number in all_fibonacci():
if not number & 1:
index -= 1
if not index:
return number
def all_fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
if __name__ == '__main__':
main()