1. 程式人生 > >尤拉計劃問題二matlab實現

尤拉計劃問題二matlab實現

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

程式碼可能不太簡潔,拋磚引玉。希望大家能多多交流!予以指正!