Problem 12. Fibonacci sequence
Solution Stats
Problem Comments

17 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
The size 10 solutions all involve a regexp hack that essentially injects arbitrary code of size 1. The following is size 10:
function f=fib(n)
f=1;
return
Bottom line, anything of size 10 is sneaking past a weakness in the size algorithm.
Here's a size 23 legit solution I came up with. It's based on expressing F(n+2)F(n+1)+F(n) in the form of a 2x2 matrix, [1 1;1 0]. It turns out that using that form explicitly, I got down to size 24. That matrix can be expressed one size smaller as mod(pascal(2),2), 2pascal(2), or magic(2)~=2, and likely other forms. And OBTW, it works for zero and negative numbers as well!
function f = fib(n)
f=(2pascal(2))^n;
f=f(2);
end
Good explanation of the problem. I am new to cody but I was able to understand easily the input and output format of the problem.
function y = fib(n)
y = ((((1+sqrt(5))/2)^n)(((1sqrt(5))/2))^n)/sqrt(5);
y=abs(y);
end
would you please give me hint that why it doesn't work ?
Solution Comments

1 Comment
I am new to cody and in general new to programming. Is it advisable to try and write my own logic or is it completely ok to take help from google. In this case I could have used the binet's formula which I found on google which I wasn't familiar with earlier.

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

2 Comments
why the function fibonacci is not working here?
Because fibonacci is a function in the Symbolic Math Toolbox. Cody only support base MATLAB but none of its toolboxes.

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

4 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!
Nice !

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 Solvers11727
Suggested Problems

867 Solvers

Solve the set of simultaneous linear equations
359 Solvers

671 Solvers

932 Solvers

729 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!