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
4
votes
2 answers

Find all the primes from 2 to n, using the Sieve of Eratosthenes

I have a word problem I am trying to solve but am getting stuck on a key part. Convert the following English description into Python code. Initialize n to be 100. Initialize numbers to be a list of numbers from 2 to n, but not including n. With…
Matt
  • 75
  • 1
  • 8
4
votes
4 answers

Sieve of Eratosthenes with Wheel Factorization

I'm implementing a reasonably fast prime number generator and I obtained some nice results with a few optimizations on the sieve of Eratosthenes. In particular, during the preliminary part of the algorithm, I skip all multiples of 2 and 3 in this…
4
votes
2 answers

Why am I getting segfault from this unsigned int?

I'm trying to initialize an integer array and set all elements to 1. I need the array to have an upper bound of 4294967295, or the maximum number possible for a 32-bit unsigned int. This seems like a trivial task to me, and it should be, but I am…
idigyourpast
  • 714
  • 4
  • 12
  • 24
3
votes
4 answers

Sieve of Eratosthenes Scheme

I've been searching the web for an implementation of the Sieve of Eratosthenes in scheme and although I came up with a lot of content, none of them seemed to have made it like I need it to be done. The problem is most algorithms either use a static…
WiseOlMan
  • 926
  • 2
  • 11
  • 26
3
votes
5 answers

Improving efficiency of Sieve of Eratosthenes code in java

I've worked upon a code for Sieve of Eratosthenes in Java, but I face a few time and space efficiency issues. Here's the code : import java.util.*; class EratosthenesSeive { public static void main(String args[]) { …
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
3
votes
2 answers

Spoj PRIME1 using sieve of eratosthenes (in C)

I'm trying to solve the problem PRIME1 using segmented sieve of Eratosthenes. My program works correctly with the normal sieve that is up to NEW_MAX. But there is some problem with cases n > NEW_MAX, where segmented sieving comes into play. In such…
ksb
  • 670
  • 8
  • 19
3
votes
1 answer

Why is this Sieve of Sundaram implementation much faster than this Sieve of Eratosthenes implementation?

I'm currently trying to compare the average runtime speed of two different prime numbers generating algorithms. I have this naive implementation of the Sieve of Eratosthenes: std::vector sieve_of_eratosthenes(int32_t n) { …
3
votes
1 answer

Sieve of Eratosthenes run slower after improving

I try to improve basic Sieve of Eratosthenes algorithm by avoiding cross out duplicate multiple of primes, but it turn out to be worse than my expectation I has implemented two method that return primes in range [2..max) Basic sieve public static…
KevinBui
  • 880
  • 15
  • 27
3
votes
2 answers

Postponed Sieve algorithm with start logic

Based on this Python answer by Will Ness, I've been using a JavaScript adaptation for the postponed sieve algorithm from that answer: function * primes() { yield 2; yield 3; yield 5; yield 7; const sieve = new Map(); const ps…
vitaly-t
  • 24,279
  • 15
  • 116
  • 138
3
votes
2 answers

Sieve of Eratosthenes - bitwise optimization problem

I guess every one of you has encountered the optimization code of Eratosthenes sieve with bitwise operations. I am trying to wrap my head around it and I have a question on one of the operations in this implementation. Here is the code from…
3
votes
3 answers

Unexpected behavior with iterators

I tried to implement the sieve of Eratosthenes with iterators (because I wanted to get more into functional programming with python). Sadly some unexpected behaviour occurred. You can see it here in this video:…
3
votes
1 answer

Parallelizing Sieve of Eratosthenes in Java

I am trying to make a parallel implementation of the Sieve of Eratosthenes. I made a boolean list which gets filled up with true's for the given size. Whenever a prime is found, all multiples of that prime are marked false in the boolean list. The…
SD33N
  • 88
  • 7
3
votes
1 answer

What's the best data structure to use for a sieve (i.e. list of numbers where some get crossed out)?

I am implementing the Sieve of Eratosthenes. I am stuck on the first step: deciding which data structure to use. Put simply, the Sieve of Eratosthenes starts with a list of consecutive numbers (e.g. 2,3,4,5,...99, 100). Then, certain numbers are…
3
votes
2 answers

Sieve of Eratosthenes - C language implementation

I am missing something but I can't find what it is. I have also been given a input2.c file and it has a print_prim function which I am not allowed to change. For n=10 it is always printing 4, 5, 7, 9, I know there is an i+2 in print_prim function…
keser
  • 2,472
  • 1
  • 12
  • 38
3
votes
2 answers

F#: inefficient sequence processing

I have the following code to perform the Sieve of Eratosthenes in F#: let sieveOutPrime p numbers = numbers |> Seq.filter (fun n -> n % p <> 0) let primesLessThan n = let removeFirstPrime = function | s when Seq.isEmpty s -> None …