13 May 2011

Euler Project2 欧拉工程2



Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

求斐波那契数列中小于4,000,000的值中的偶数项之和.
%% sum of even value elements from fibonacci list smaller than 4,000,000
% Elapsed time is 0.000011 seconds.
% ans =
% 4613732
function result = euler2()
tic;
a = 1;
b = 2;
result = 2;
while (a<4000000 && b<4000000) a = a+b; if mod(a,2)==0 result = result+a; end b = a+b; if mod(b,2)==0 result = result+b; end end toc; end

No comments :

Post a Comment