Search This Blog

#Day100#100DaysChallenge- Matlab Loops |Sum of Even Fibonacci Series -Matlab Cody

#Day100-Sum of Even Fibonacci Series-Matlab Cody

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 the input value, find the sum of the even-valued terms.
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab Code:

function y = euler002(x)

t1=1;

t2=2;

y=0;

      while t1<=x

      yy=t1;

      temp=t1;

      t1=t2;

      t2=temp+t2;

      if rem(yy,2)==0

          y=y+yy;        

      end

      end

end

Sample Input and Output

>> x =597455000 y_correct = 350704366; assert(isequal(euler002(x),y_correct))
Test Suite -Pass.

Click here for video Description



No comments