Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Fibonacci Series in cody Challenge

2 ビュー (過去 30 日間)
NAVNEET NAYAN
NAVNEET NAYAN 2018 年 7 月 30 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I am solving Fibonacci series in Cody challenge. I have written the following code and it is running well in MATLAB editor. here is the code
function f = fib(n)
if (n>2)
f(1)=1;
f(2)=1;
for i=3:n
f(i)=f(i-1)+f(i-2);
end
elseif n==2
f(1)=1;
f(2)=1;
elseif (n==1)
f(1)=1;
else
disp('invalid input')
end
end
However when I am submitting it in cody incorrect answer is displayed. Can anyone suggest it why this one is running in editor and wrong when submitted?
  2 件のコメント
Kaitlyn Keil
Kaitlyn Keil 2018 年 7 月 30 日
I'm not positive since the original question isn't displayed, but if this is the question:
Calculate the nth Fibonacci number.
Given n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...
then it might be that you are doing too much, and they only want the final number, not all of the proceeding ones. Try just returning the last one maybe?
NAVNEET NAYAN
NAVNEET NAYAN 2018 年 7 月 31 日
Yes, you are right. They wanted only last number. I was trying too much.
Thanks

回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by