× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Is this working properly - Sum of Fibonacci in Python 3

Martin Czygan
May 12, 2015
<p>The sums of the first ten and 100 fibonacchi number would be 88 and 573147844013817084100, respectively:</p> <pre><code>&gt;&gt;&gt; cache = {} &gt;&gt;&gt; def fib(n): if n == 0: return 0 if n == 1: return 1 if not n in cache: cache[n] = fib(n - 1) + fib(n - 2) return cache[n] &gt;&gt;&gt; sum([fib(i) for i in range(10)]) 88 &gt;&gt;&gt; sum([fib(i) for i in range(100)]) 573147844013817084100 </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30128330/Is%20this%20working%20properly%20-%20Sum%20of%20Fibonacci%20in%20Python%203/30128474">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus