Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: Java 8 Streams and Eratosthenes Date: Tue, 04 Jun 2013 17:24:30 -0700 Organization: A noiseless patient Spider Lines: 29 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 5 Jun 2013 00:19:58 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="5aa64ebf9a53573f97c75abba936b0e2"; logging-data="21916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hyGK8Ca7DvnzC8cz4EMKvbuDVJNSjL4E=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: Cancel-Lock: sha1:CVDBJebBYIggp6ARfhquppFB0PM= Xref: csiph.com comp.lang.java.programmer:24293 On 6/4/2013 2:54 PM, Sebastian wrote: > Hello there, > > I'd like to start an open discussion on issues involving the Streams > API. As an exercise, I have looked at generating the first n primes, > for some given n, using the sieve of Eratosthenes. It seemed a > plausible enough proposition, seeing that the sieve involves repeatedly > filtering a sequence of integers. I don't think it's plausible or a good implementation. The point of this particular sieve is that it relies on easily skipping past values and only accessing the one's you want. For example, in an array, you can easily compute the even numbers by adding +2 to the previous offset. Multiples of 3 are found by adding +3 to the previous offset. Etc. In order to determine if n is prime, you have to run n filters on all previous values of n. This seems inefficient. With streams, you have to account for the fact that the previous sieve has already removed a lot of values. (I assume; I'm no expert on Java lambdas.) How do you even do that? I don't know. I think I agree with your conclusion that "Perhaps the lesson is that some things are best not done using streams." That's likely the case imo. Perhaps a better candidate is BigInteger#nextProbablePrime().