Calculate the nth Fibonacci number.
Given n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...
Examples:
Input n = 5 Output f is 5
Input n = 7 Output f is 13
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
Is there a shorter way?
Yes.
Also this solutions will fail fib(0)
I broke Cody server with this.
function f = fib(n)
a=[0 1];
indx=n-1;
if n==0
f=a(1);
elseif n==1
f=a(2);
else
while indx <= n
a(indx+1)=fib(n-1)+fib(n-2);
indx=indx+1;
end
f=a(n);
end
also my size is -1 shouldnt I win?
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
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.
nice task
nice
why the function fibonacci is not working here?
Can someone explain about the solution size please..
good job
this is too easy
The Test Suite should include a check using random integer inputs to prevent simple indexing to generate a solution.
i think there is no mistake in my codes.......
very easy
very easy
.
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.
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.
Fibonacii series os that series in which each digit is sum of the previous digits
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) = ...
Not sure what went wrong...
Put this at the end (between the two 'end's)
f=f(n);
:)
I didn't even think about going the recursive route. Seems obvious now.
I am using the property of squared fibonacci numbers.
alignment is such a problem here :)
Yay recursion
Testsuite seems to have problem for value n=1
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!
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.
This is broken: in my haste to fix a typo in my first version, I accidentally replaced the wrong 2 with a 1.
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 =)
This is good answer. Better than 'round(1.61803^n /2.236). The latter is wrong if n>24.
recursion in programming ............
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.
We finally get into recursion to generate Fibonacci
i want to know how does the second line work , i don't understand the reslut ,can u help???,
A clever idea, knowing that the determinant of the dramadah matrix obeys a Fibonacci series
This solution doesn't work after n=23.
It's still one hell of a hack! :)
678 Solvers
953 Solvers
175 Solvers
The sum of the numbers in the vector
349 Solvers
369 Solvers