可能的重复:
用于查找斐波那契数列的 Python 程序。更多 Pythonic 方式。
嘿,我正在尝试编写一个脚本,将“斐波那契数列”中的所有偶数项求和到 400 万以下。
Fibonacci1 = 1
Fibonacci2 = 2
a = 2
i = 4
for i in range(1,4000000):
Fibonacci1 = Fibonacci1 + Fibonacci2
if Fibonacci1 % 2 == 0:
a = a + Fibonacci1
Fibonacci2 = Fibonacci1 + Fibonacci2
if Fibonacci2 % 2 == 0:
a = a + Fibonacci2
print a
raw_input()
本来应该不到一分钟的时间,但是花了一整晚都没有解决!
编辑:对不起,我误解了这个问题。我认为这意味着我必须将所有偶数项相加至 400 万!但解决方案是将所有偶数项相加,直到 400 万。
工作代码(不到一秒完成):
Fibonacci1 = 1
Fibonacci2 = 2
a = 2
while a < 4000000:
Fibonacci1 = Fibonacci1 + Fibonacci2
if Fibonacci1 % 2 == 0:
a = a + Fibonacci1
Fibonacci2 = Fibonacci1 + Fibonacci2
if Fibonacci2 % 2 == 0:
a = a + Fibonacci2
print a
raw_input()
Possible Duplicate:
Python program to find fibonacci series. More Pythonic way.
Hey, i was trying to write a script which sums all the even terms in "Fibonacci Sequence" under 4 millions.
Fibonacci1 = 1
Fibonacci2 = 2
a = 2
i = 4
for i in range(1,4000000):
Fibonacci1 = Fibonacci1 + Fibonacci2
if Fibonacci1 % 2 == 0:
a = a + Fibonacci1
Fibonacci2 = Fibonacci1 + Fibonacci2
if Fibonacci2 % 2 == 0:
a = a + Fibonacci2
print a
raw_input()
it should takes less than a minute, but it took all night and it wasn't solved !
Edit: Sorry guys, i misunderstood the problem. I though it means that i have to sum all the even terms UP TO 4 million ! But the solution was to sum all the even terms UNTIL 4 million.
Working code (finished in less than one second):
Fibonacci1 = 1
Fibonacci2 = 2
a = 2
while a < 4000000:
Fibonacci1 = Fibonacci1 + Fibonacci2
if Fibonacci1 % 2 == 0:
a = a + Fibonacci1
Fibonacci2 = Fibonacci1 + Fibonacci2
if Fibonacci2 % 2 == 0:
a = a + Fibonacci2
print a
raw_input()
发布评论
评论(10)
您的代码存在一些问题:
大多数人在开始学习 Python 时只学习命令式编程。这并不奇怪,因为 Python 是一种命令式语言。但Python也在一定程度上支持函数式编程,对于这种练习,我认为函数式编程方法更具启发性。
首先定义一个生成所有斐波那契数的生成器:
要使用这个生成器,我们可以从 itertools 导入一些有用的函数。要打印前几个数字,请使用 islice:
仅查找偶数我们可以使用 ifilter 生成一个新的生成器:
获取数字从生成器直到数量超过四百万,使用 takewhile:
解决您可以使用 sum 的问题:
我希望这表明函数式编程方法并不困难,并且是解决某些类型问题的更优雅的方法。
There are a couple of problems with your code:
Most people when they start learning Python learn only imperative programming. This is not surprising because Python is an imperative language. But Python also supports functional programming to a certain extent and for this sort of exercise a functional programming approach is more enlightening in my opinion.
First define a generator that generates all the Fibonacci numbers:
To use this generator we can import a few useful functions from itertools. To print the first few numbers use islice:
To find only the even numbers we can use ifilter to produce a new generator:
To fetch numbers from the generator until a number exceeds four million use takewhile:
To solve the problem you can use sum:
I hope this shows that a functional programming approach is not difficult and is a more elegant way to solve certain types of problem.
你确定你要的是
i
小于400万吗?Are you sure it is
i
that you want to be less than 4 million?您可能有兴趣了解整数序列在线百科全书!
您可以按名称或按顺序搜索信息。
如果您搜索 0,2,8,34 或“偶数斐波那契”,您将被重定向到序列 A014445
在那里你可以找到很多信息,包括公式,
由此编写一个直接生成偶数斐波那契数的生成器很容易。
You may be interested in knowing about the The On-Line Encyclopedia of Integer Sequences!
You can search information by name or by sequence.
If you search either for 0,2,8,34 or 'Even Fibonacci' you will be redirect to sequence A014445
There you you find lots of information including formulas,
from that to code a generator that will yield the even fibonacci numbers directly is easy.
循环条件不对,应该是这样的:
The loop condition is wrong, it should be something like this:
目前,您对所有前 200 万个偶数斐波那契数求和。
但这不是任务。您必须将所有低于 400 万的偶数斐波那契数相加。
Currently, you sum up all the first 2 million even Fibonacci numbers.
But that is not the task. You have to sum all even Fibonacci numbers that are below 4 million.
注意:这已被大量修改以解决实际问题
这是一种替代方法(非常快,但未经测试的方法)。
它依赖于一些属性:
因此转换我们的最大值 4000000 计算最高斐波那契数的索引。
小于它。
33 可以被 3 整除,所以它是一个偶数斐波那契数,如果不是偶数,我们需要像这样调整 n。
到目前为止所有斐波那契数的总和(如果由下式给出)
所有偶数的总和是此数的一半:
这样做的好处是,如果包含成本,它的复杂度为 O(1) (或 O(log(N)) pow/log) 算法,并且适用于双打..因此我们可以计算非常大的值的总和。
NOTE: This has been heavily modified to address the actual question
Here's an alternate (and very fast, but untested, method).
It relies on a few properties:
So convert our maximum value of 4000000 calculate the index of the highest Fibonacci number
less than it.
33 is divisible by 3 so it is an even Fibonacci number, if it wasn't we'd need to adjust n like this.
The sum of all Fibonacci numbers up to this point if given by
The sum of all even numbers is half of this:
Nice thing about this is that its an O(1) (or O(log(N)) if you include the cost of pow/log) algorithm, and works on doubles.. so we can calculate the sum for very large values.
我不太明白你的算法,但似乎smerriman有一个观点,4000000个斐波那契数列肯定会增长到4M以上。我想更快的方法是生成高达 4M 的序列,然后将偶数相加。
I don't exactly understand your algorithm, but it seems that smerriman has a point, 4000000 Fibonnacci sequence numbers would surely grow above 4M. I guess that the faster approach would be to generate sequence up to 4M, then adding up the even ones.
还有其他技巧可以使这一过程比简单地计算斐波那契数列的完整列表,然后对列表中的偶数求和更有效。
如果是我试图解决这个问题,我会列出偶数斐波那契数列。该列表有什么有趣的特征吗?你能说服自己这种模式总体上是正确的吗?
接下来,我可能会寻找一种方法来计算该列表的成员,并且仅计算那些需要的元素。我什至可能会看看是否有任何公式可以得出斐波那契数列的总和。
当然,所有这些都会占用您更多的时间,而不是简单地编写暴力解决方案,但欧拉项目就是为了找到漂亮的解决方案而不是暴力解决方案。最后,如果你学到了一些关于数学、关于计算的知识,那么你就已经有所收获。
There are other tricks that make this more efficient than simply computing the complete list of Fibonacci numbers, and then summing the even numbers in the list.
I would, IF it were me trying to solve this problem, make a list of the even Fibonacci numbers. Are there any interesting characteristics of that list? Can you convince yourself that this pattern holds true in general?
Next, I might look for a way to compute the members of that list, and only those elements that are needed. I might even look to see if there are any formulas to be found that yield the sum of a sequence of Fibonacci numbers.
Of course, all of this would take up more of your own time than simply coding up the brute force solution, but Project Euler is all about finding the pretty solution rather than the brute force solution. In the end, if you have learned something about mathematics, about computation, then you have gained.
来吧,计算斐波那契数列的最佳方法是使用 2 个技巧:
已编辑,对之前的错误表示歉意
1:
矩阵 N*M^(n/3) 具有前 n 个斐波那契数的总和 (仅过滤到偶数) 是右上元素。
2:
您可以使用 http://en.wikipedia.org/wiki/Exponentiation_by_squaring,因为矩阵乘法是一个环。
所以你可以在 O(log n) 内解决我们的问题
Come on, the optimal way to compute Fibonacci sequence is using 2 tricks:
EDITED, sorry for earlier mistake
1:
Matrix N*M^(n/3) has sum of first n fibonacci numbers (filtered only to the even ones) is the upper right element.
2:
You can use http://en.wikipedia.org/wiki/Exponentiation_by_squaring, because matrix multiplication is a ring.
So you can solve our problem in O(log n)