Question

Use a for loop in Matlab to create an array containing the first 1,000 Fibonacci numbers....

Use a for loop in Matlab to create an array containing the first 1,000 Fibonacci numbers. Then determine how many of those numbers are divisible by 3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In fibonacci series t(1)=1,t(2)=1 and t(n)=t(n-1)+t(n-2)

Matlab code for the above program explained in comments:

%Declare an empty array
x=[];
%Insert 1 two times in it
t1=1;
t2=2;
x=[x,t1];
x=[x,t2];
%Now for remaining 998 fibonacci numbers iterate thorugh the loop
for i = 1:1:998
tmp=t2;
t2=t1+t2;
t1=tmp;
x=[x,t2];
end
%Initialize count as 0
count=0;
%Iterate through array x and if the number is divisibe by 3 then increment count
for i=1:1000
if mod(x(i),3)==0
count=count+1;
end
end
%Print total fibonacci numbers divisible by 3
fprintf("Total fibonacci numbers divisible by 3= %d",count);

Output:
octave:18> source(my_script.m) Total fibonacci numbers divisible by 3= 846

Total fibonaaci numbers divisible by 3 are 846

Add a comment
Know the answer?
Add Answer to:
Use a for loop in Matlab to create an array containing the first 1,000 Fibonacci numbers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT