TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The real way to generate a list of primes in Haskell

76 pointsby garrisonjabout 10 years ago

16 comments

jkarniabout 10 years ago
Funnily, there&#x27;s haskell-cafe thread[0], a github issue, and <i>even a paper</i>, about this (and I think maybe reddit got involved too).<p>Anyhow, the title is kind of too much. At least, given the aforementioned discussions, we&#x27;re conflicted liars.<p>[0]<a href="https:&#x2F;&#x2F;mail.haskell.org&#x2F;pipermail&#x2F;haskell-cafe&#x2F;2015-April&#x2F;119428.html" rel="nofollow">https:&#x2F;&#x2F;mail.haskell.org&#x2F;pipermail&#x2F;haskell-cafe&#x2F;2015-April&#x2F;1...</a> [2]<a href="http:&#x2F;&#x2F;www.cs.hmc.edu&#x2F;~oneill&#x2F;papers&#x2F;Sieve-JFP.pdf" rel="nofollow">http:&#x2F;&#x2F;www.cs.hmc.edu&#x2F;~oneill&#x2F;papers&#x2F;Sieve-JFP.pdf</a> [3] <a href="https:&#x2F;&#x2F;github.com&#x2F;haskell-infra&#x2F;hl&#x2F;pull&#x2F;8" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;haskell-infra&#x2F;hl&#x2F;pull&#x2F;8</a>
评论 #9544205 未加载
评论 #9543048 未加载
rifungabout 10 years ago
As much as I hate the title of this post, I think the author has a point.<p>I&#x27;ve also seen this same thing come up when comparing implementations of quicksort in Haskell to that of other languages. They always show a short, elegant implementation in Haskell, but the issue is that it&#x27;s not really quicksort as it doesn&#x27;t do the sort in place.
评论 #9542618 未加载
ColinWrightabout 10 years ago
This has always bothered me about most implementations of the Sieve of Eratosthenes. Namely, what they produce isn&#x27;t it.<p>If you have division operations, or &quot;mod&quot; operations, it&#x27;s not the Sieve of Eratosthenes, it&#x27;s just a filter.<p>Not the same thing at all.
评论 #9543961 未加载
btillyabout 10 years ago
Why is Haskell so slow at this?<p>As far as I can tell, my Perl implementation at <a href="http:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=276112" rel="nofollow">http:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=276112</a> is doing something similar with similar amounts of laziness and no optimizations build it. Yet I can produce the first 50,000 primes in the time that this takes to produce the first 10,000. And nobody uses Perl for its speed!
评论 #9542123 未加载
teabout 10 years ago
You can get another ~3x speedup by implementing the three lines of code comprising the &quot;simple wheel&quot; described at bottom of page 8 of the referenced O&#x27;Neill paper.
gohrtabout 10 years ago
This result was more famously previously published by Melissa E. O’Neill as<p><a href="https:&#x2F;&#x2F;www.cs.hmc.edu&#x2F;~oneill&#x2F;papers&#x2F;Sieve-JFP.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.hmc.edu&#x2F;~oneill&#x2F;papers&#x2F;Sieve-JFP.pdf</a><p>The Genuine Sieve of Eratosthenes<p>Harvey Mudd College, Claremont, CA, U.S.A. (e-mail: oneill@acm.org)<p>And is available on Hackage as:<p><a href="https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;primes-0.2.1.0&#x2F;docs&#x2F;Data-Numbers-Primes.html" rel="nofollow">https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;primes-0.2.1.0&#x2F;docs&#x2F;Data...</a>
Chinjutabout 10 years ago
I agree that this is a more efficient way of generating primes in Haskell than the typical Haskell 101 approach. However, I disagree with the idea that the Haskell 101 approach does not deserve to be called an implementation of the &quot;Sieve of Eratosthenes&quot;.<p>The distinction is only this: when we have found a prime p and are eliminating numbers accordingly, do we consider ourselves only to spend time directly enumerating the multiples of p and crossing them off? Or do we consider ourselves as running through the entire list and going &quot;Ok, ok, cross, ok, ok, cross, ok, ok, cross&quot; (for, for example, p = 3), thus spending time traversing through multiples and non-multiples alike? So to speak, do we jump from &quot;cross&quot; to &quot;cross&quot;, or do we walk along through the &quot;ok&quot;s inbetween?<p>In the former case, each new candidate is worked on only in proportion to its number of prime factors; in the latter case, each new candidate is worked on in proportion to all smaller primes. The former is the more efficient way of generating primes; the latter is (essentially) the ubiquitous, naive approach.<p>But I don&#x27;t think one can say the traditional understanding of the Sieve of Eratosthenes draws a strong distinction between these two! Traditional accounts would not explicate any difference between &quot;Jump directly from &#x27;cross&#x27; to &#x27;cross&#x27; &quot; and &quot;Walk from &#x27;cross&#x27; to &#x27;cross&#x27;, saying &#x27;ok&#x27; to everything inbetween&quot;. It&#x27;s not a distinction anyone was traditionally worried about. Eratosthenes certainly didn&#x27;t.<p>So I think both of these are deserving of the name &quot;Sieve of Eratosthenes&quot;. They&#x27;re just different approaches to that sieve.<p>In either case, we say there are primes, to each prime we associate the set of its multiples, we merge these sets into the set of composites, and close the loop of our recursion by noting that the primes are to be the complement of these composites. The difference is, in some sense, arising just from how we represent and manipulate subsets of the naturals (as pertaining to the set of multiples of each prime, as well as their merger into the totality of composites): either as streams of increasing naturals [efficient], or as streams of &quot;In&quot;s and &quot;Out&quot;s [less efficient].
jamesromabout 10 years ago
Where is it implied on the Haskell home page that it&#x27;s the Sieve of Eratosthenes?<p>The variable name? It&#x27;s a sieve.<p><a href="http:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Sieve_theory" rel="nofollow">http:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Sieve_theory</a>
codygmanabout 10 years ago
Upvote despite the article being titled &quot;Haskell programmers are liars&quot;. Great decision to use subtitle admins. +1 to bitemyapp for submitting a PR to change &quot;sieve&quot; to &quot;primeFilter&quot; and avoid this in the future.
pathikritabout 10 years ago
My Scala one using a Java BitSet: <a href="https:&#x2F;&#x2F;github.com&#x2F;pathikrit&#x2F;scalgos&#x2F;blob&#x2F;9bd0dd81df52a5a410c2e6844c5ac0ba62cf544e&#x2F;src&#x2F;main&#x2F;scala&#x2F;com&#x2F;github&#x2F;pathikrit&#x2F;scalgos&#x2F;NumberTheory.scala#L19-L26" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pathikrit&#x2F;scalgos&#x2F;blob&#x2F;9bd0dd81df52a5a410...</a>
wycabout 10 years ago
Even if you know the most optimal solution in terms of computational complexity, it might not be the best thing to put into your code base. There are a lot of other things to balance including (but not limited to) readability, maintainability, probability of correctness, and of course developer time. Considering these multiple dimensions is essential to good engineering.<p>I&#x27;m not saying you shouldn&#x27;t know the best algorithms for a problem, as this article clearly demonstrates the effectiveness of a more efficient solution. In fact, having better understanding of algorithms and computational complexity makes it safer for you to accurately assess the trade-offs you&#x27;ll be making by picking slower but simpler code or faster code with more complexity. There is more to consider than just big-O when writing software.<p>Note: What I&#x27;m saying most strongly applies to software with functionality that doesn&#x27;t exist yet. If there&#x27;s a reliable library with what you&#x27;re seeking (such as a way to generate primes), it&#x27;s usually best to use it.
thegeomasterabout 10 years ago
I might be missing something obvious so excuse me, but why not use a heap as a priority queue? It has O(1) find-minimum and O(log n) insert, which is better than a set which is probably some kind of self-balancing BST (I don&#x27;t speak Haskell).
评论 #9541742 未加载
评论 #9541743 未加载
petermoraabout 10 years ago
Looks the same as in Clojure&#x27;s lazy-seq documentation <a href="https:&#x2F;&#x2F;clojuredocs.org&#x2F;clojure.core&#x2F;lazy-seq" rel="nofollow">https:&#x2F;&#x2F;clojuredocs.org&#x2F;clojure.core&#x2F;lazy-seq</a>
评论 #9543035 未加载
fiboabout 10 years ago
I wrote this with osfameron&#x27;s help <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;fibo&#x2F;1203756" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;fibo&#x2F;1203756</a>
sjbrabout 10 years ago
Shame on them!
LukeHoerstenabout 10 years ago
That&#x27;s an overly broad and sensational title. The simplified example on the website certainly isn&#x27;t representative of all Haskell programmers being liars. It&#x27;s unfortunate because the article about mis-implementations of the Sieve of Eratosthenes is decent.<p>Edit: mods, thanks for changing the misleading title.
评论 #9541571 未加载
评论 #9541576 未加载