Effacer les filtres
Effacer les filtres

Help with a simple exercise

1 vue (au cours des 30 derniers jours)
Gimmy Morales
Gimmy Morales le 30 Août 2013
In this exercise Im asked to show the Fibonacci numbers < 100... but when I run the program it shows me one > 100 (the 144), The 144 can't be there, what can I do? (Sorry for my english)
Here is what I got:
F(1)= 1; F(2)= 1;
n= 3; c= 0;
while c < 100;
F(n)= F(n-1) + F(n-2);
c= F(n);
n= n+1;
end
F

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Août 2013
Your code keeps running as long as c < 100, and goes ahead and computes and stores the next in sequence when that is true.
Suppose that it had generated 99 (I know it doesn't, but for the sake of explanation), then because 99 < 100, it would go on one more step and calculate the one after 99 -- which could not be < 100 considering there are no terms that involve adding 0 or negatives.
What you need to do is test for < 100 after you have done the calculation, but before you store into F, and do not store into F if it would be >= 100
  1 commentaire
Gimmy Morales
Gimmy Morales le 30 Août 2013
Thanks, very helpful.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by