Problem 12. Fibonacci sequence
Solution Stats
Problem Comments

20 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 ?
Why no "elseif" on test 1? this messed with my recursive solution, but I worked around it with some return statements.
@Brendan It's most likely an attempt to make lookup solutions that merely encode the return values for all the tests (and which are widely considered cheating) that little bit harder. I do agree that this is questionable insofar as that banning elseif in particular also messes with genuine solutions.
Good one
Solution Comments
Show commentsProblem Recent Solvers13186
Suggested Problems

1183 Solvers

Relative ratio of "1" in binary number
1380 Solvers

convert matrix to single column
402 Solvers

4751 Solvers

Find the next Fibonacci number
549 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!