Questions tagged [sieve-of-eratosthenes]

Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer.

Sieve of Eratosthenes finds primes among the natural numbers above 1 between the composites, which it finds by direct enumeration from each prime (optimization: prime's square), as an arithmetic progression with the step equal to that prime.

The first prime number is 2.

In pseudocode, the timing issues aside, it is

primes = [2, 3, ...] \ [[p*p, p*p+p, ...] for p in primes]

430 questions
100
votes
4 answers

Time complexity of Sieve of Eratosthenes algorithm

From Wikipedia: The complexity of the algorithm is O(n(logn)(loglogn)) bit operations. How do you arrive at that? That the complexity includes the loglogn term tells me that there is a sqrt(n) somewhere. Suppose I am running…
Lazer
  • 79,569
  • 109
  • 264
  • 349
75
votes
19 answers

Sieve of Eratosthenes - Finding Primes Python

Just to clarify, this is not a homework problem :) I wanted to find primes for a math application I am building & came across Sieve of Eratosthenes approach. I have written an implementation of it in Python. But it's terribly slow. For say, if I…
Srikar Appalaraju
  • 66,073
  • 51
  • 206
  • 260
41
votes
6 answers

Segmented Sieve of Eratosthenes?

It's easy enough to make a simple sieve: for (int i=2; i<=N; i++){ if (sieve[i]==0){ cout << i << " is prime" << endl; for (int j = i; j<=N; j+=i){ sieve[j]=1; } } cout << i << " has " << sieve[i] << "…
John Smith
  • 9,380
  • 16
  • 43
  • 49
39
votes
11 answers

Speed up bitstring/bit operations in Python?

I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def…
36
votes
16 answers

The sieve of Eratosthenes in F#

I am interested in an implementation of the sieve of eratosthenes in purely functional F#. I am interested in an implementation of the actual sieve, not the naive functional implementation that isn't really the sieve, so not something like this: let…
IVlad
  • 41,273
  • 11
  • 102
  • 170
32
votes
27 answers

Program to find prime numbers

I want to find the prime number between 0 and a long variable but I am not able to get any output. The program is using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication16 { class…
sandy101
  • 3,195
  • 3
  • 20
  • 19
23
votes
7 answers

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

I have been trying to write Sieve of Eratosthenes algorithm in JavaScript. Basically I just literally followed the steps below: Create a list of consecutive integers from 2 to (n-1) Let first prime number p equal 2 Starting from p, count up in…
Bao
  • 587
  • 2
  • 8
  • 17
23
votes
5 answers

Sieve of Atkin explanation

I am doing a project at the moment and I need an efficient method for calculating prime numbers. I have used the sieve of Eratosthenes but, I have been searching around and have found that the sieve of Atkin is a more efficient method. I have found…
marc lincoln
  • 1,385
  • 4
  • 20
  • 25
22
votes
13 answers

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Eratosthenes method, implemented it, and changed the question title and tags appropriately. …
eleven81
  • 7,551
  • 10
  • 34
  • 47
17
votes
12 answers

Sieve of Eratosthenes in Erlang

I'm in the process of learning Erlang. As an exercise I picked up the Sieve of Eratosthenes algorithm of generating prime numbers. Here is my code: -module(seed2). -export([get/1]). get(N) -> WorkList = lists:duplicate(N, empty), get(2,…
Roskoto
  • 1,567
  • 1
  • 14
  • 27
15
votes
5 answers

Sieve of Eratosthenes in Ruby

Rather than scraping a Ruby version of this algorithm off the net I wanted to create my own based on its description here. However I cannot figure out two things def primeSieve(n) primes = Array.new for i in 0..n-2 primes[i] = i+2 end …
Damian
  • 653
  • 1
  • 9
  • 12
12
votes
1 answer

Clojure: Avoiding stack overflow in Sieve of Erathosthene?

Here's my implementation of Sieve of Erathosthene in Clojure (based on SICP lesson on streams): (defn nats-from [n] (iterate inc n)) (defn divide? [p q] (zero? (rem q p))) (defn sieve [stream] (lazy-seq (cons (first stream) …
nixx
  • 121
  • 2
11
votes
2 answers

Understanding the Limitations of Lazy Evaluation (Sieve of Eratosthenes)

In the Haskell Wiki article on prime numbers, the following implementation of the Sieve of Eratosthenes is described: primes = 2 : 3 : minus [5,7..] (unionAll [[p*p, p*p+2*p..] | p <- tail primes]) When doing... primes !! 2 ... how does Haskell…
11
votes
3 answers

Why do I fail Project Euler #10?

Question is: Find the sum of all the primes below 2 million. I pretty much did the Sieve of Erastothenes thing, and the program below seems to work for small number i.e. define LIMIT as 10L produces 17 as answer. I submitted 1179908154 as the…
idazuwaika
  • 2,039
  • 5
  • 31
  • 40
11
votes
4 answers

Why is this prime test so slow?

This code was taken from the book "Haskell Road to Logic, Math and Programming". It implements sieve of eratosthenes algorithm and solves Project Euler Problem 10. sieve :: [Integer] -> [Integer] sieve (0 : xs) = sieve xs sieve (n : xs) = n : sieve…
anatoly
  • 270
  • 2
  • 8
1
2 3
28 29