Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a simple tail-recursive solution:</p> <pre><code>def fibonacci(n: Long): Long = { def fib(i: Long, x: Long, y: Long): Long = { if (i &gt; 0) fib(i-1, x+y, x) else x } fib(n, 0, 1) } </code></pre> <p>The solution you posted takes <em>exponential</em> time since it creates two recursive invocation trees (<code>fibonacci(n - 1)</code> and <code>fibonacci(n - 2)</code>) at each step. By simply tracking the last <em>two</em> numbers, you can recursively compute the answer without any repeated computation.</p> <hr> <blockquote> <p>Can you explain the middle part, why <code>(i-1, x+y, x)</code> etc. Sorry if I am asking too much but I hate to copy and paste code without knowing how it works.</p> </blockquote> <p>It's pretty simple—but my poor choice of variable names might have made it confusing.</p> <ul> <li><code>i</code> is simply a counter saying how many steps we have left. If we're calculating the <em>Mth</em> (I'm using <code>M</code> since I already used <code>n</code> in my code) Fibonacci number, then <code>i</code> tells us how many more terms we have left to calculate before we reach the <em>Mth</em> term.</li> <li><code>x</code> is the <em>mth</em> term in the Fibonacci sequence, or <em>F<sub>m</sub></em> (where <code>m = M - i</code>).</li> <li><code>y</code> is the <code>m-1th</code> term in the Fibonacci sequence, or <em>F<sub>m-1</sub></em> .</li> </ul> <p>So, on the first call <code>fib(n, 0, 1)</code>, we have <code>i=M</code>, <code>x=0</code>, <code>y=1</code>. If you look up the <a href="http://en.wikipedia.org/wiki/Fibonacci_number#List_of_Fibonacci_numbers" rel="nofollow">bidirectional Fibonacci sequence</a>, you'll see that F<sub>0</sub> = 0 and F<sub>-1</sub> = 1, which is why <code>x=0</code> and <code>y=1</code> here.</p> <p>On the next recursive call, <code>fib(i-1, x+y, x)</code>, we pass <code>x+y</code> as our next <code>x</code> value. This come straight from the definiton:</p> <p><em>F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub></em></p> <p>We pass <code>x</code> as the next <code>y</code> term, since our current F<sub>n-1</sub> is the same as F<sub>n-2</sub> for the next term.</p> <p>On each step we decrement <code>i</code> since we're one step closer to the final answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload