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)-(((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 ?
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 look-up 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 Solvers13235
Suggested Problems
-
Remove the small words from a list of words.
1490 Solvers
-
340 Solvers
-
Numbers with prime factors 2, 3 and 5.
469 Solvers
-
949 Solvers
-
459 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!