Calculate the nth Fibonacci number.
Given n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...
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
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.
give me a new think about numbers
function f = fib(n)
c =[1 2];
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.
why the function fibonacci is not working here?
Can someone explain about the solution size please..
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.......
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)
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 :)
I admit it's a bit of a cheat.
i don't know how to cheat. Could you tell me?
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 ?
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)).
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.
Even though there are smaller solutions, I prefer this one.
nice one :)
Wow! How did you come up with this?!
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! :)
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office