尤拉計劃問題二matlab實現
阿新 • • 發佈:2018-11-10
Problem 2:Even Fibonacci numbers
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;
end
s
程式碼可能不太簡潔,拋磚引玉。希望大家能多多交流!予以指正!