Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main problem with your Python-to-Java translation is that Python's assignment statement up there is executed all at once, while Java's are executed serially. Python's statement is equivalent to saying this:</p> <pre><code>Make a list out of 'b' and 'a + b' Make another list out of references to 'a' and 'b' Assign all the elements from the second list to the first one </code></pre> <p>(It might actually be a tuple, I'm not exactly fluent in Python.)</p> <p>So the 'b' and 'a+b' resolve to values before they are assigned. You can't do that kind of multiple-simultaneous assignment in Java.</p> <p>In general, a statement in Python like</p> <pre><code>var1, var2, ...varN = expression1, expression2, ...expressionN </code></pre> <p>is going to translate in Java to</p> <pre><code>temp1 = expression1; temp2 = expression2; ... tempN = expressionN; var1 = temp1; var2 = temp2; ... varN = tempN; </code></pre> <p>This way all the expressions resolve to values <strong>before</strong> the assignments happen, and none of the assignments have side effects on the expressions.</p> <p>If I were doing this for real I'd probably do the lookup table and store longs (since Fibonacci numbers grow vaguely exponentially and I'd want to go past 46). The iterative form, like you have, will take O(N) to calculate the Nth Fibonacci value; the typical recursive formulation will take as many function calls as the returned value. Fibonacci practically begs for the answers to be cached somewhere, and this would make the recursive form much more feasible.</p>
    singulars
    1. This table or related slice is empty.
    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. 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