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]

469 questions
9
votes
3 answers

Clojure - tail recursive sieve of Eratosthenes

I have this implementation of the sieve of Eratosthenes in Clojure: (defn sieve [n] (loop [last-tried 2 sift (range 2 (inc n))] (if (or (nil? last-tried) (> last-tried n)) sift (let [filtered (filter #(or (= % last-tried) (<…
9
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,749
  • 7
  • 38
  • 46
9
votes
4 answers

Cannot understand this prime generator algorithm in my textbook

I am studying Elements of Programming Interviews, and I am stuck on a problem. It is about writing a c++ function for finding all prime numbers from 1 to n, for a given n. vector generate_primes_from_1_to_n(const int &n) { int size =…
Eric
  • 2,635
  • 6
  • 26
  • 66
9
votes
5 answers

Factorizing a number in Python

Here's my code: def factorize(n): sieve = [True] * (n + 1) for x in range(2, int(len(sieve) ** 0.5) + 1): if sieve[x]: for i in range(x + x, len(sieve), x): sieve[i] = False lowerPrimes = i for i in…
9
votes
1 answer

"The Genuine Sieve of Eratosthenes" in Python - why is heapq slower than dict?

Following M. O'Neill's great paper, I tried implementing some lazy, infinite versions of the Sieve of Eratosthenes in Python. I was surprised to find that the heap-based version, that the paper claims should run faster, was actually over two times…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
9
votes
6 answers

Prime Sieve in Haskell

I'm very new to Haskell and I'm just trying to find the sum of the first 2 million primes. I'm trying to generate the primes using a sieve (I think the sieve of Eratosthenes?), but it's really really slow and I don't know why. Here is my code.…
9
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
  • 350
  • 2
  • 8
8
votes
4 answers

An Efficient Sieve of Eratosthenes in Python

This very short and simple code in #Python tries to simulate the "Sieve of Eratosthenes" for the first N natural numbers with the constraints of (0) script shortness; (1) minimization of the 'if statements' and 'for/while loops'; (2) efficiency in…
Rob
  • 135
  • 2
  • 6
8
votes
7 answers

Prime Number Algorithm

Can anyone tell me how to implement Sieve of Eratosthenes algorithm in C? I need to generate prime numbers but my algorithm is slow. My code: #include int prime(long int i) { long int j; int state = 1; for(j=2;j
jaykumarark
  • 2,359
  • 6
  • 35
  • 53
8
votes
5 answers

Java 8: streams and the Sieve of Eratosthenes

The Sieve of Eratosthenes can be implemented very neatly in Haskell, using laziness to generate an infinite list and then remove all multiples of the head of the list from its tail: primes :: [Int] primes = sieve [2..] sieve (x:xs) = x : sieve [y |…
user1636349
  • 458
  • 1
  • 4
  • 21
8
votes
3 answers

Is there a fast, functional prime generator?

Suppose I've got a natural number n and I want a list (or whatever) of all primes up to n. The classic prime sieve algorithm runs in O(n log n) time and O(n) space -- it's fine for more imperative languages, but requires in-place modification to…
8
votes
2 answers

C - Sieve of Eratosthenes - BitField

I'm about to implement the Sieve of Eratosthenes and have a general question regarding the sieve-array. I've implemented the sieve quite a few times now (in C) and always used an array of uint8_t (out of ) as the sieve. This is pretty…
Matthias
  • 175
  • 6
8
votes
2 answers

A Fast Prime Number Sieve in Python

I have been going through prime number generation in python using the sieve of Eratosthenes and the solutions which people tout as a relatively fast option such as those in a few of the answers to a question on optimising prime number generation in…
cobie
  • 7,023
  • 11
  • 38
  • 60
7
votes
6 answers

No of Pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion

How to find number of pairs of consecutive prime numbers having difference of 6 like (23,29) from 1 to 2 billion (using any programming language and without using any external libraries) with considering time complexity? Tried sieve of…
Anand
  • 85
  • 2
  • 7
7
votes
3 answers

Multithreaded Segmented Sieve of Eratosthenes in Java

I am trying to create a fast prime generator in Java. It is (more or less) accepted that the fastest way for this is the segmented sieve of Eratosthenes: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. Lots of optimizations can be further…
1
2
3
31 32