Generating Fibonacci Sequence Using While Loop
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nicholas
am 5 Okt. 2014
Kommentiert: Austin Marking
am 17 Mär. 2021
Hello all,
I am trying to generate the first Fibonacci Sequence Term greater than 1000 using a while loop. I am using the following code:
fibf(1) = 1;
fibf(2) = 1;
n=3:50;
while fibf(n) < 1000
fibf(n) = fibf(n-1)+fibf(n-2);
end
I am getting the error, 'Index exceeds matrix dimensions'. Any help is appreciated
1 Kommentar
Akzeptierte Antwort
Azzi Abdelmalek
am 5 Okt. 2014
fibf(1) = 1;
fibf(2) = 1;
n=3
while fibf(n-1) < 1000
fibf(n) = fibf(n-1)+fibf(n-2);
n=n+1;
end
3 Kommentare
Arvindhan Sayapathy
am 9 Sep. 2017
To get values exactly less than 1000, you can change the while condition to:
while(fibf(n - 1) + fibf(n - 2) < 1000)
Weitere Antworten (1)
NEHA THAKUR
am 2 Apr. 2020
fibf(1) = 1;
fibf(2) = 1;
n=3
while fibf(n-1) < 1000
fibf(n) = fibf(n-1)+fibf(n-2);
n=n+1;
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!