Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to take the writing into the file out of the fibonacci_fast method. Each time you call it, it re-opens the file and, since you didn't close it, there is no guarantee that it will be written into the file. </p> <p>As for speed, recursion is not a good fit for your calculations. You don't need the recursive call:</p> <pre><code>res = fibonacci_fast(n-1) + fibonacci_fast(n-2) </code></pre> <p>because you could have already come up with the return value of those calls. It would be better to start from the beginning and working until you get to the value that you want, without suffering the overhead caused by this kind of recursion. In other words, it would be faster to take an iterative approach. </p> <p>As you are probably trying to show, generating the n-th number in the Fibonacci sequence is not a good candidate for recursion, in the sense of speed/optimization. </p> <p>If you change your fibonacci_fast to something like:</p> <pre><code>def fibonacci_fast(n): known = [0, 1] count = 0 for i in range(n): newNum = known[0] + known[1] known[0] = known[1] known[1] = newNum return known[0] </code></pre> <p>and running your test script:</p> <pre><code>def main(): time_start = time.time() print fibonacci_slow(20) time_end = time.time() print "Time for slow fibonacci to complete ", time_end - time_start time_start = time.time() print fibonacci_fast(20) time_end = time.time() print "Time for fast fibonacci to complete ", time_end - time_start </code></pre> <p>you get:</p> <pre><code>6765 Time for slow fibonacci to complete 0.0043318271637 6765 Time for fast fibonacci to complete 0.00010085105896 </code></pre> <p>and to write it to a file you can add a file argument which you can write to:</p> <pre><code>def fibonacci_fast(n, f): known = [0, 1] for i in range(n): newNum = known[0] + known[1] known[0] = known[1] known[1] = newNum f.write(str(known[0]) + " ") return known[0] </code></pre> <p>and call it like so:</p> <pre><code>f = open("fib.txt", "w") fibonacci_fast(20, f) f.close() </code></pre> <p>or the more 'pythonic' way (which is a little bit faster):</p> <pre><code>with open("fib.txt", "w") as f: fibonacci_fast(20, f) </code></pre> <p>You can see the big difference between the recursive and iterative approach if you try to generate the 500th number in the sequence. It takes minutes (if not hours, I haven't waited it out) to finish the recursive function but only a fraction of a fraction of a second to do the iterative method, even with writing it to a file.</p> <p>More information on the fibonacci sequence can be found <a href="https://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python?rq=1">here.</a></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.
 

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