Questions tagged [sieve]

Sieves are a type of algorithm used e.g. in finding primes with the sieve of Eratosthenes, sieve of Atkin etc. "Sieving" refers to gradual dismissal of directly generated sequences of numbers as possible candidates, until only the desired numbers are left.

Sieves are a type of algorithm used e.g. in finding primes with the sieve of Eratosthenes, sieve of Atkin etc., finding lucky numbers, or in integer factorization. "Sieving" the candidate numbers refers to gradual dismissal of directly generated sequences of (in that context, composite) numbers as possible candidates, until only the desired (e.g. prime) numbers are left.

For the unrelated email filtering language, see .

198 questions
5
votes
5 answers

Making Sieve of Eratosthenes more memory efficient in python?

Sieve of Eratosthenes memory constraint issue Im currently trying to implement a version of the sieve of eratosthenes for a Kattis problem, however, I am running into some memory constraints that my implementation wont pass. Here is a link to the…
Fredrik HD
  • 105
  • 6
5
votes
3 answers

How do I implement the Sieve Of Eratosthenes using multithreaded C#?

I am trying to implement Sieve Of Eratosthenes using Mutithreading. Here is my implementation: using System; using System.Collections.Generic; using System.Threading; namespace Sieve_Of_Eratosthenes { class Controller { public…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
5
votes
3 answers

Why wouldn't my sieve terminate when I rewrote it as a foldl?

What's the specific problem with my foldl that prevents it from terminating or producing output? First I achieved a sieve for primes. It's not the best, but it works just fine as (for example) take 20 primesA. primesA :: [Integer] primesA = sieve 2…
minopret
  • 4,726
  • 21
  • 34
4
votes
3 answers

Sieve of Eratosthenes in Javascript vs Haskell

I've been playing around with Haskell and find it fascinating, especially the Lazy Evaluation feature, which allows us to work with (potentially) infinite lists. From this, derives the beautiful implementation of the Sieve of Eratosthenes to get an…
4
votes
0 answers

Sieve of Euler space complexity in Haskell

I was given 2 different algorithms written in Haskell aimed to generate the first k primes. As the title suggests, they are the Sieve of Eratoshenes and Euler. I am trying to understand why Euler implementation uses so much memory. What I thought so…
4
votes
3 answers

Why are these sieve optimizations breaking my code?

And how can I correct them to work? I am trying to optimize my sieve per previous suggestions, but in both instances the code breaks: Incrementing j = j + ( i * 2) will break the code. Obviously I'm missing some concept on optimizations as well as…
jennifer
  • 682
  • 5
  • 14
4
votes
1 answer

Is it possible to come up with a distributed / multi core implementation of a prime sieve.

I have been working on prime sieve algorithm, and the basic implementation is working fine for me. What I am currently struggling with is a way to divide and distribute the calculation on to multiple processors. I know it would require storage of…
user3386925
4
votes
1 answer

Remove every (k+1) th remaining element in kth pass of natural numbers

In the natural numbers series, we've to remove every 2nd element in the 1st pass. Then in the remaining elements, remove every 3rd element in the second pass. Then at Kth pass, remove every (k+1)th element from the remaining elements. The series…
viji
  • 2,706
  • 5
  • 28
  • 34
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

How to look for factorization of one integer in linear sieve algorithm without divisions?

I learned an algorithm called "linear sieve" https://cp-algorithms.com/algebra/prime-sieve-linear.html that is able to get all primes numbers smaller than N in linear time. This algorithm has one by-product since it has an array lp[x] that stores…
Dachuan Huang
  • 115
  • 1
  • 8
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
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
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
4 answers

Count of divisors of numbers till N in O(N)?

So, we can count divisors of each number from 1 to N in O(NlogN) algorithm with sieve: int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) { cnt[j]++; //// here cnt[x] means count of divisors of x } } Is…
Barish Namazov
  • 340
  • 1
  • 2
  • 13
3
votes
1 answer

Long long int makes my Sieve of Eratosthenes super slow?

I have a program that requires me to find primes up till 10**10-1 (10,000,000,000). I wrote a Sieve of Eratosthenes to do this, and it worked very well (and accurately) as high as 10**9 (1,000,000,000). I confirmed its accuracy by having it count…
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
1
2
3
13 14