Problem 12. Fibonacci sequence
Solution Stats
Problem Comments

14 Comments
very basic problem
basic... but where is the description of the Fibonacci series?
lim_n>(inf) of fib(n+1)/fib(n)=golden ratio :0
((((((1+sqrt(5)))^n)(((1sqrt(5)))^n)))/2*n*sqrt(5))
Only size matters?
I have tested tic toc time for 4 kinds of solutions I have seen for this problem. I have computed 1e5 fibonacci numbers f(n) with n<=70 picked at random. Here are the results:
1) Explicit formula (sltn 408159, size 42): 3.83e01 s. \\
2) for loop (sltn 409425, size 36): 1.09e01 s. \\
3) filter (sltn 409380, size 33): 4.78e01 s. \\
4) Recursion (sltn 408916, size 31): inifinity
fibonacci, a math out of gods creation
I remember we did this in highschool ))
Really the most awful problem. Add f=whateverucalledthevector(n) at the end to make the script work.
Good Problem.
easy
Interesting one!
give me a new think about numbers
I am unsure why this code is not working.
function f = fib(n)
f(1)=1;
f(2)=1;
for i=3:n
f(i)=f(i1)+f(i2);
end
age=f(n);
X=['f is ',num2str(age)];
disp(X)
end
this is cool xd
Solution Comments

1 Comment
easy. classical iteration problem. solving this problem is easy, but iteration is hard to master.

2 Comments
hahahahah
Classic!

2 Comments
Is there a shorter way?
Yes.
Also this solutions will fail fib(0)

2 Comments
I broke Cody server with this.
function f = fib(n)
a=[0 1];
indx=n1;
if n==0
f=a(1);
elseif n==1
f=a(2);
else
while indx <= n
a(indx+1)=fib(n1)+fib(n2);
indx=indx+1;
end
f=a(n);
end
also my size is 1 shouldnt I win?
When you want to calculate fib(1), it calls fib(0) + fib(1). There is no fib(1) so you never leave the while loop.

2 Comments
function f = fib(n)
c =[1 2];
for i=1:n
c(i+2)=c(i)+c(i+1)
end
f=c(:,i)
end
could anybody please give me hint. I am struck here
f(1) fails. All other cases passed
change c=[1 2] to c=[1 1]

1 Comment
i wrote a code just like i would in c++. The code is huge by size but the answers are right however cody seems to disagree.

1 Comment
nice task

1 Comment
nice

1 Comment
why the function fibonacci is not working here?

1 Comment
Can someone explain about the solution size please..

1 Comment
good job

1 Comment
this is too easy

1 Comment
The Test Suite should include a check using random integer inputs to prevent simple indexing to generate a solution.

1 Comment
i think there is no mistake in my codes.......

1 Comment
very easy

1 Comment
very easy

1 Comment
.

1 Comment
i had to add a line of code because you didnt expect the number to be a float rather then an integer so it counted my last test as wrong all because it had .000 at the end of each one.

2 Comments
I tried this one in MATLAB and it worked but here it keeps displaying assertion failed for every number different from 1
Because you are using floating point calculations, the result may not be exact. If you round the result to the nearest integer it should work.

1 Comment
Fibonacii series os that series in which each digit is sum of the previous digits

2 Comments
Can anyone suggest how to reduce the size of the code ?
Yes there are 2 ways. You can initialise f1 as a vector [1, 1]. You also don't need the if else statement if you use start i = 1:n and then use f1(i+2) = ...

2 Comments
Not sure what went wrong...
Put this at the end (between the two 'end's)
f=f(n);

1 Comment
:)

1 Comment
I didn't even think about going the recursive route. Seems obvious now.

1 Comment
I am using the property of squared fibonacci numbers.

1 Comment
alignment is such a problem here :)

1 Comment
Yay recursion

2 Comments

1 Comment
Testsuite seems to have problem for value n=1

3 Comments
it is really a mental challenge
I don't understand why the size of this code is so huge, can anyone help ?
Hi all,
I have used regular expressions in other languages before but not in MATLAB. Can someone explain the solution to me? I have looked up the format of regexp in the help file, but the use of the regexp in the solution does not seem to fit the description in help.
When I try and run the code in Octave it falls over with an error as it does not recognise the with an 'Invalid call to regexp' error.
Can someone please help!

2 Comments
problem in the test suite my Solution was perfect, please update the test suite
I'm not sure how you reach that conclusion. The example given says fib(5) should be 5. Your code returns 15.

1 Comment
This is broken: in my haste to fix a typo in my first version, I accidentally replaced the wrong 2 with a 1.

3 Comments
I don't get why this isn't correct.
I got the equation from 1) and the calculations are correct when I use Matlab at home (see 2)).
1) https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form
2) https://en.wikipedia.org/wiki/Fibonacci_number#List_of_Fibonacci_numbers
this would work if you returned floor(f).
Thank you =)

1 Comment
This is good answer. Better than 'round(1.61803^n /2.236). The latter is wrong if n>24.

1 Comment
recursion in programming ............

3 Comments
Not really my work; the previous leader did this, I simply reduced the size by 2 using the 'ans trick'.
This is not really the correct solution, as for large n it will eventually produce inaccurate results.
True. It can be improved a bit though by using more decimals for phi.

3 Comments

1 Comment
We finally get into recursion to generate Fibonacci

1 Comment
i want to know how does the second line work , i don't understand the reslut ,can u help???,

1 Comment
A clever idea, knowing that the determinant of the dramadah matrix obeys a Fibonacci series

2 Comments
This solution doesn't work after n=23.
It's still one hell of a hack! :)
Problem Recent Solvers10916
Suggested Problems

1019 Solvers

Is my wife right? Now with even more wrong husband
1273 Solvers

299 Solvers

309 Solvers

Area of an equilateral triangle
3819 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!