这个没太弄明白为啥m=n-m
%%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.
s=0;
m = 1;
n = 2;
while n < 4000000
if mod(n,2)==0 %偶数项
s = s + n; %求和
end
n = n + m; %迭代
m = n -m; %为啥不是m=n
end
s

浙公网安备 33010602011771号