TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Fibonacci series and Dynamic programming

38 点作者 aditgupta将近 12 年前

9 条评论

blt将近 12 年前
The fastest way to calculate the Fibonacci sequence (with integer math) is a simple loop, just like you&#x27;d do it on paper. This version is slower <i>and</i> much more complicated. With so many relevant good examples of recursion and dynamic programming, it boggles my mind to see the Fibonacci sequence used as an example for either.<p><pre><code> def fib(n): a, b = 0, 1 for k in range(n): a, b = b, a + b return a</code></pre>
评论 #5893553 未加载
评论 #5893768 未加载
评论 #5893568 未加载
评论 #5894061 未加载
crntaylor将近 12 年前
I can&#x27;t resist a quick plug for this beautiful O(n) implementation of the fibonacci sequence in Haskell. One line to define the list `fibs` -- an infinite list of fibonacci numbers -- and one line to define the function `fib` which simply takes the n&#x27;th element of the list.<p><pre><code> λ&gt; let fibs = 0 : 1 : zipWith (+) fibs (tail fibs) λ&gt; let fib n = fibs!!n λ&gt; fib 100 354224848179261915075</code></pre>
评论 #5894040 未加载
评论 #5894362 未加载
tmoertel将近 12 年前
Perhaps surprisingly, you can convert many recursive algorithms into equivalent iterative versions using a series of simple mechanical steps. For example, if we start with the naive recursive implementation of our beloved fib function,<p><pre><code> def fib(n): if n &lt; 2: return n return fib(n - 1) + fib(n - 2) </code></pre> we can arrive at the iterative (dynamic-programming) version that follows,<p><pre><code> def fib(n): fibn1, fibn2 = (0, 1) for _ in xrange(n): fibn1, fibn2 = fibn2, fibn1 + fibn2 return fibn1 </code></pre> through the following steps: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;tmoertel&#x2F;5798134" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;tmoertel&#x2F;5798134</a><p>If you&#x27;re interested in this kind of thing, I&#x27;m writing a series of articles on converting recursive algorithms into iterative algorithms. The initial article: <a href="http:&#x2F;&#x2F;blog.moertel.com&#x2F;posts&#x2F;2013-05-11-recursive-to-iterative.html" rel="nofollow">http:&#x2F;&#x2F;blog.moertel.com&#x2F;posts&#x2F;2013-05-11-recursive-to-iterat...</a>
评论 #5894315 未加载
评论 #5895002 未加载
peter_l_downs将近 12 年前
Or you could apply some math and come up with a generating function!<p><a href="http:&#x2F;&#x2F;www.math.ufl.edu&#x2F;~joelar&#x2F;generatingfibonacci.pdf" rel="nofollow">http:&#x2F;&#x2F;www.math.ufl.edu&#x2F;~joelar&#x2F;generatingfibonacci.pdf</a>
评论 #5894935 未加载
评论 #5895078 未加载
prakashk将近 12 年前
Perl 6 (from [1]):<p><pre><code> &gt; my @fib := 1, 1, *+* ... *; @fib[99] 354224848179261915075 &gt; (1, 1, *+* ... *)[99] 354224848179261915075 </code></pre> [1] <a href="https:&#x2F;&#x2F;justrakudoit.wordpress.com&#x2F;2010&#x2F;12&#x2F;29&#x2F;perl-6-fibonacci-versus-haskell&#x2F;" rel="nofollow">https:&#x2F;&#x2F;justrakudoit.wordpress.com&#x2F;2010&#x2F;12&#x2F;29&#x2F;perl-6-fibonac...</a>
pathikrit将近 12 年前
A O(n) Scala one: <a href="https:&#x2F;&#x2F;github.com&#x2F;pathikrit&#x2F;scalgos&#x2F;blob&#x2F;master&#x2F;src&#x2F;main&#x2F;scala&#x2F;com&#x2F;github&#x2F;pathikrit&#x2F;scalgos&#x2F;Combinatorics.scala#L100" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pathikrit&#x2F;scalgos&#x2F;blob&#x2F;master&#x2F;src&#x2F;main&#x2F;sc...</a>
jgeerts将近 12 年前
Reminds me awefully much about the blog post below.<p><a href="http:&#x2F;&#x2F;agillo.net&#x2F;getting-groovy-with-fibonacci&#x2F;" rel="nofollow">http:&#x2F;&#x2F;agillo.net&#x2F;getting-groovy-with-fibonacci&#x2F;</a>
评论 #5893607 未加载
dhruvchandna将近 12 年前
In Clojure you can implement this as:<p><pre><code> (def fib-list (map first (iterate (fn [[a b]] [b (+ a b)]) [0N 1N]))) (nth fib-list 100) 354224848179261915075N</code></pre>
amckinlay将近 12 年前
Doesn&#x27;t Clojure have a .memoize() that would be useful here?