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)-(((1-sqrt(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.83e-01 s. \\
2) for loop (sltn 409425, size 36): 1.09e-01 s. \\
3) filter (sltn 409380, size 33): 4.78e-01 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(i-1)+f(i-2);
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), 2-pascal(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=(2-pascal(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)-(((1-sqrt(5))/2))^n)/sqrt(5);
y=abs(y);
end
would you please give me hint that why it doesn't work ?
Solution Comments
Show commentsProblem Recent Solvers12529
Suggested Problems
-
Maximum running product for a string of numbers
1931 Solvers
-
Remove the polynomials that have positive real elements of their roots.
1458 Solvers
-
Back to basics 23 - Triangular matrix
939 Solvers
-
351 Solvers
-
Find the largest value in the 3D matrix
1428 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!