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
3
votes
1 answer

Sieve of Eratosthenes on a segment

Sieve of Eratosthenes on the segment: Sometimes you need to find all the primes that are in the range [L...R] and not in [1...N], where R is a large number. Conditions: You are allowed to create an array of integers with size…
Anand Singh
  • 87
  • 10
3
votes
1 answer

Sieve of Erosthenes much slower when called as function in Python

I have two blocks of code, both of which I have written to apply the sieve of eratosthenes to sum all primes up to 2000000. This first block, which is just raw code not wrapped in any function, is this: N = 2000000 is_prime = (N + 1) * [True] for…
EthanS
  • 115
  • 7
3
votes
3 answers

Implementing the Page Segmented Sieve of Eratosthenes in Javascript

I recently read about a faster implementation of Segmented Sieve of Eratosthenes for really big numbers. Following is an implementation of the same: function sieve(low, high) { var primeArray = [], ll = Math.sqrt(low), output = []; for…
Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38
3
votes
2 answers

Prime-sieve optimized for memory

I am looking for a prime-sieve implementation which is efficient in terms of memory consumption. Of course, the primality-test itself should execute at a constant and minimal number of operations. I have implemented a sieve that indicates primality…
barak manos
  • 29,648
  • 10
  • 62
  • 114
3
votes
1 answer

Prime sieve results in (me going to) stack overflow

For project euler i was looking for a way to implement the sieve of Eratosthenes because i expected it to be faster than my original implementation (and needed it to be so). I came up with this function: sieve :: Int -> [Int] -> [Int] sieve p (x:xs)…
Wokkelsok
  • 140
  • 1
  • 6
3
votes
3 answers

Prime Number generator with recursion and list comprehension

I am a newbie to Haskell programming and have trouble understanding how the below list comprehension expands. primes = sieve [2..] sieve (p:xs) = p : sieve [x | x <-xs, x `mod` p /= 0] Can someone correct me how the sieve expansion works: As we…
Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47
3
votes
2 answers

Sieve of Eratosthenes has huge 'overdraw' - is Sundaram's better after all?

The standard Sieve of Eratosthenes crosses out most composites multiple times; in fact the only ones that do not get marked more than once are those that are the product of exactly two primes. Naturally, the overdraw increases as the sieve gets…
DarthGizka
  • 4,347
  • 1
  • 24
  • 36
3
votes
1 answer

Not printing desired output for sieve of eratosthene

I'm trying to create a program which outputs the list of prime numbers given an input value, n. The SieveEratosthenes function that I made: - generates a list of primes over the first n integers - creates storage for the list of generated primes -…
user3291818
  • 203
  • 1
  • 3
  • 11
3
votes
2 answers

Sieve of Eratosthenes algorithm in C

Okay, so this function I created uses the Sieve of Eratosthenes algorithm to compute all the primes <= n. This function stores the prime numbers and the count of primes in the parameters. When the function exits, primes should be pointing to a…
3
votes
5 answers

C#: How to make Sieve of Atkin incremental

I don't know if this is possible or not, but I just gotta ask. My mathematical and algorithmic skills are kind of failing me here :P The thing is I now have this class that generates prime numbers up to a certain limit: public class Atkin :…
Svish
  • 152,914
  • 173
  • 462
  • 620
3
votes
5 answers

Prime number in C

int prime(unsigned long long n){ unsigned val=1, divisor=7; if(n==2 || n==3) return 1; //n=2, n=3 (special cases). if(n<2 || !(n%2 && n%3)) return 0; //if(n<2 || n%2==0 || n%3==0) return 0; for(; divisor<=n/divisor; val++,…
Painguy
  • 565
  • 6
  • 13
  • 25
2
votes
5 answers

Dynamic Sieve Algorithms for Prime Generation

I'm implementing the Sieve of Eratosthenes, for an explanation of this see http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. However I would like to adapt it to generate M primes, not the primes 1 through N. My method of doing this is simply to…
daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
2
votes
2 answers

Function to find all prime numbers at most n in Racket

I'm still pretty fresh to Racket so a bit confused about this, i've created a drop-divisible and sieve-with function as shown below with some help but now need to use both to create a single function that finds all prime numbers with a given length…
bakesy09
  • 21
  • 2
2
votes
2 answers

Sieve of Eratosthenes in Scheme using mutation of local state in its filtering procedure

While answering a recent question I came up with the following code, implementing a variant of the sieve of Eratosthenes, repeatedly culling the initial 2...n sequence, stopping as early as possible: (define (sieve2 n) (let ((ls (makelist n))) …
Will Ness
  • 70,110
  • 9
  • 98
  • 181
2
votes
2 answers

Is there a way to move labels up on a sieve plot in order to make the graphs look cleaner?

I am working on a research project and I am trying to show my boss some clean graphs that can be put into a project. I made this sieve plot and I can't figure out 1. How to rotate the x-axis labels and make pull them off the graph and 2. How to move…
Sam Cole
  • 57
  • 6
1 2
3
13 14