Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 18th, 2009, 08:59 PM
Newbie
 
Join Date: Oct 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
savni98 is on a distinguished road
Default matlab code asap thanks Fibonacci sequence

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, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.


what is the matlab code



I have no idea what to do i got this so far and i do not know if its right???




f=zeros(1,4000000);
f(1)=1;
f(2)=1;
for j= 3:4000000
f(j)=f(j-1)+f(j-2);
end
sum(f<4000000)
Reply With Quote
Advertisement
 
  #2  
Old October 18th, 2009, 09:50 PM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 11,394
Country:
Thanks: 667
Thanked 3,621 Times in 2,918 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

Quote:
Originally Posted by savni98 View Post
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, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.


what is the matlab code



I have no idea what to do i got this so far and i do not know if its right???




f=zeros(1,4000000);
f(1)=1;
f(2)=1;
for j= 3:4000000
f(j)=f(j-1)+f(j-2);
end
sum(f<4000000)
First you are generating the numbers from the wrong starting point, you are told to use f(1)=1, f(2)=2, but no matter it works better starting with f(1)=1, f(2)=1.

You don't need to store the numbers, and you have to sum every third one, the following may work (its untested):

Code:
a=1;b=1;

sum=0;                %running total of the even Fib. numbers
for idx=3:4000000     %loop over a sufficient range no get a Fib. number over 4000000
  n=a+b;              %next Fib number
  a=b;b=n;            %reset last two numbers for next trip arround loop
  if n>4000000        %exit loop if current number >4000000
    break
  end
  if mod(idx,3)==0    %if this is an even Fib number add so running total
    sum=sum+n;
  end
end
CB
__________________
Truth does not change because it is, or is not, believed by a majority of the people.

Giordano Bruno
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 01:59 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.