Generating Fibonacci Sequence Using While Loop
13 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Azzi Abdelmalek
2014-10-5
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 个评论
Arvindhan Sayapathy
2017-9-9
To get values exactly less than 1000, you can change the while condition to:
while(fibf(n - 1) + fibf(n - 2) < 1000)
更多回答(1 个)
NEHA THAKUR
2020-4-2
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!