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
Azzi Abdelmalek 2014 年 10 月 5 日

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 件のコメント

Umesh Pandey
Umesh Pandey 2016 年 8 月 19 日
編集済み: Umesh Pandey 2016 年 8 月 19 日
fibf(1)=0 ? also it gives one value more than 1000.
Arvindhan Sayapathy
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)
Austin Marking
Austin Marking 2021 年 3 月 17 日
Does the counter variable “n” HAVE to go second in the while loop?

サインインしてコメントする。

その他の回答 (1 件)

NEHA THAKUR
NEHA THAKUR 2020 年 4 月 2 日

1 投票

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

カテゴリ

ヘルプ センター および 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!

Translated by