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 .

171 questions
24
votes
9 answers

Java 8 Stream, getting head and tail

Java 8 introduced a Stream class that resembles Scala's Stream, a powerful lazy construct using which it is possible to do something like this very concisely: def from(n: Int): Stream[Int] = n #:: from(n+1) def sieve(s: Stream[Int]): Stream[Int] =…
lyomi
  • 3,820
  • 5
  • 25
  • 38
20
votes
9 answers

How do I generate Primes Using 6*k +- 1 rule

We know that all primes above 3 can be generated using: 6 * k + 1 6 * k - 1 However we all numbers generated from the above formulas are not prime. For Example: 6 * 6 - 1 = 35 which is clearly divisible by 5. To Eliminate such conditions, I…
Uma Kanth
  • 5,614
  • 2
  • 16
  • 40
15
votes
7 answers

Is there a way to find the approximate value of the nth prime?

Is there a function which will return the approximate value of the n th prime? I think this would be something like an approximate inverse prime counting function. For example, if I gave this function 25 it would return a number around 100, or if I…
David Johnstone
  • 23,034
  • 14
  • 64
  • 71
14
votes
2 answers

Question about the ~ and @ operators in Haskell

What exactly do they do? I know one possible use of @ (assigning a name at the start of a pattern match), but haven't been able to find anything on ~. I found them in the following code snippet, taken from…
damien
  • 802
  • 6
  • 12
13
votes
7 answers

Find position of prime number

I need to do the reverse of finding the Nth prime, i.e. Given a prime number, I need to find its position in 2, 3, 5, 7... The prime number can be large, in the order of 10^7. Also, there are a lot of them. I have an index of pre-calculated primes…
max
  • 3,900
  • 2
  • 22
  • 36
10
votes
4 answers

Prime number hard drive storage for very large primes - Sieve of Atkin

I have implemented the Sieve of Atkin and it works great up to primes nearing 100,000,000 or so. Beyond that, it breaks down because of memory problems. In the algorithm, I want to replace the memory based array with a hard drive based array.…
WyomingGeezer
  • 157
  • 11
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
4 answers

Haskell --> F#: Turner's Sieve

I was reading on different sieving algorithms when I stumbled upon a kind of improved version of the Sieve of Eratosthenes called Euler's Sieve. According to Wikipedia there is an implementation of an slightly different version of the idea (called…
chrischu
  • 2,931
  • 3
  • 25
  • 43
8
votes
1 answer

Sieve of Sundaram - list comprehension

I am trying to write a function that calculates all odd prime numbers from 1..n using the "Sieve of Sundaram" algorithm. Here is my try: sSund :: Integer -> [Integer] sSund n = [ i * 2 + 1 | i <- [1..n], j <- [f i], (i + j + 2 * i * j) > n ] …
7
votes
4 answers

Fastest prime test for small-ish numbers

I'm playing through project Euler in my spare time, and it's come to the point where I need to do some refactoring. I've implemented Miller-Rabin, as well as a few sieves. I've heard before that sieves are actually faster for small-ish numbers, as…
hraesvelgr
  • 3,961
  • 2
  • 32
  • 58
6
votes
1 answer

Python generators; two apparently identical programs work differently

The program below [Python 3.4] is a simple Eratosthenes sieve: from itertools import * def excl(ns,pr): return (i for i in ns if i%pr) def sieve(ns): while True: pr=next(ns) yield pr ns=excl(ns,pr) # ns=(i for…
6
votes
2 answers

Segmented Sieve of Atkin, possible?

I am aware of the fact that the Sieve of Eratosthenes can be implemented so that it finds primes continuosly without an upper bound (the segmented sieve). My question is, could the Sieve of Atkin/Bernstein be implemented in the same way? Related…
K.Steff
  • 2,012
  • 3
  • 19
  • 24
5
votes
4 answers

Sieve of Eratosthenes

I read up on the sieve of Eratosthenes while solving a question on Project Euler. I'm sure you guys know which question im talking about. So here's the thing. My code manages to show all the primes under 1 million correctly. However when i try the…
Ole Gooner
  • 557
  • 2
  • 10
  • 21
5
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…
5
votes
1 answer

J Primes Enumeration

J will answer the n-th prime via p:n. If I ask for the 100 millionth prime I get an almost instant answer. I cannot imagine J is sieving for that prime that quickly, but neither looking it up in a table as that table would be around 1GB in…
user1202733
  • 613
  • 4
  • 9
1
2 3
11 12