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
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,164
  • 3
  • 19
  • 25
5
votes
2 answers

Optimized Summation of all prime numbers under N

There was 10th question in project eular. The problem is to find the sum of all prime numbers not greater than N. My soln for the problem is : int solve(int n){ bool check[n+1]; for(int i=0;i<=n;i++){ check[i]=true; } …
5
votes
2 answers

Getting functional sieve of Eratosthenes fast

I read this other post about a F# version of this algorithm. I found it very elegant and tried to combine some ideas of the answers. Although I optimized it to make fewer checks (check only numbers around 6) and leave out unnecessary caching, it is…
primfaktor
  • 2,831
  • 25
  • 34
5
votes
3 answers

greatest divisor of a number and prime factors relation

Question is as follows : Given two numbers n and k. For each number in the interval [1, n], your task is to calculate its largest divisor that is not divisible by k. Print the sum of all these divisors. Note: k is always a prime…
user13499843
5
votes
3 answers

is Sieve of erathosthens the best algorithm to generate prime numbers from 1 to N?

I was asked this question in an interview. I implemented an algorithm using sieve of eratosthenes concept and an array. Is there a better way to go about this question For those who dont know the sieve , here is the…
tomkaith13
  • 1,717
  • 4
  • 27
  • 39
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
6 answers

Did I just prove that sieve of Eratosthenes is less efficient than trial division?

I was trying to compare the run-time speed of two algorithms: A brute-force C program to print prime numbers (10,000 numbers), and a Sieve of Eratosthenes C program (also 10,000 prime numbers). My measured run-time for the sieve algorithm was: 0.744…
5
votes
2 answers

Help understanding Sieve of Eratosthenes implementation

This is boring, I know, but I need a little help understanding an implementation of the Sieve of Eratosthenes. It's the solution to this Programming Praxis problem. (define (primes n) (let* ((max-index (quotient (- n 3) 2)) (v…
harto
  • 89,823
  • 9
  • 47
  • 61
5
votes
3 answers

What's the ideal implementation for the Sieve of Eratosthenes between Lists, Arrays, and Mutable Arrays?

In Haskell, I've found three simple implementations of the Sieve of Eratosthenes on the Rosetta Code page. Now my question is, which one should be used in which situations? Correcting my initial reasoning would be helpful too: I'm assuming the List…
Hudon
  • 1,636
  • 18
  • 28
5
votes
6 answers

Program to find all primes in a very large given range of integers

i came across this following question on a programming website : Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number…
Rohit Agrawal
  • 161
  • 1
  • 6
  • 16
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
2 answers

Missing small primes in C++ atomic prime sieve

I try to develop a concurrent prime sieve implementation using C++ atomics. However, when core_count is increased, more and more small primes are missing from the output. My guess is that the producer threads overwrite each others' results, before…
Krokeledocus
  • 139
  • 5
4
votes
2 answers

How can I improve performance on my clojure sieve of eratosthenes algorithm?

I'm learning clojure by going through project euler and am working on problem number 10 (find the sum of all the prime number below two million. I implemented a pretty literal algorithm for the sieve of eratosthenes but it works far too slowly to be…
Scott
  • 41
  • 1
4
votes
1 answer

Is there a way to optimize this code for finding the divisors of a number?

I've written a program in Julia to compute the divisors of a number n efficiently. The algorithm is original (as far as I know), and is loosely based on the Sieve of Eratosthenes. It essentially works like this: For a given prime p, let p^k || n;…
4
votes
2 answers

Help understanding eratosthenes sieve implementation

I found this LINQ implementation of the eratosthenes sieve on this website. I understand the basic concept of the sieve, but there's one detail I don't get. What is the purpose of the first Enumerable.Range(0,168)? List erathostheness =…
Dan
  • 257
  • 1
  • 3
  • 11