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
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

What is the best algorithm to find number of factors of first N natural numbers?

I have to find total number of factors for all numbers from 2 to N. Here's my approach. Run Sieve of Eratosthenes and get all primes from 2 to N. For each number from 2 to N, do the prime factorisation and get the exponents of all the prime factors.…
4
votes
2 answers

Efficiency of Sieve of Eratosthenes algorithm

I am trying to understand the "Sieve of Eratosthenes". Here is my algorithm (code below), and a list of features that I cannot understand (in order). Why is i * i more efficient than i * 2? Yes, I can understand it would be less iterations,…
Rami Chasygov
  • 2,714
  • 7
  • 25
  • 37
4
votes
4 answers

Scala: iterate a sequence while modifying it?

I'm trying to implement the Sieve of Eratosthenes in Scala. I start by initializing a sequence of all odd numbers plus 2: // (end goal is to find all prime factors of bigNumber) val largestPrime : Long = Math.ceil(Math.sqrt(bigNumber)).toLong var…
Ricket
  • 33,368
  • 30
  • 112
  • 143
4
votes
1 answer

Sieve of Eratosthenes returns large composite number (which is an error)

I'm implementing the Sieve of Eratosthenes in Python. It returns composite numbers near the end of the search range: def primes_Ero(n=1000): primes = [] a = [True]*(n+1) a[0] = a[1] = False for (i,isprime) in enumerate(a): …
4
votes
5 answers

Sieve of Eratosthenes in Java: A Puzzle and some optimization

I did a quick implementation of the SoE algo in Java (code at the end). The output on my dual core AMD processor is: Allocation: 31 Meat: 10140 Listing: 10171 Preparing end: 10187 The "Meat" section consumes the Maximum…
user504674
4
votes
3 answers

c++ sieve of Eratosthenes my code is slow

I'm trying to find the number of prime numbers below 400 million but even with just 40 million my code is taking 8 secs to run. what am i doing wrong? what can i do to make it faster? #include #include #include using…
Nikhil
  • 71
  • 7
4
votes
2 answers

Sieve of Eratosthenes infinite list

Hello i have to implement a function for sieve of Eratosthenes. I already have a function removep p l that removes elements of l that match the predicate p and a function nats that returns an infinite list of naturals numbers, and i am supposed to…
eeKat88
  • 106
  • 1
  • 7
4
votes
1 answer

Why does function apply complain about long lists?

As part of some Eulerian travails, I'm trying to code a Sieve of Eratosthenes with a factorization wheel. My code so far is: (defun ring (&rest content) "Returns a circular list containing the elements in content. The returned list starts with the…
4
votes
2 answers

Sieve of Eratosthenes set-implementation confusion

I wanted to first preface that I'm a python-newbie and that I'm gracious for anyone who can explain it to my clearly and completely. I was looking at the code found in the link below: http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Python I've…
Roger Josh
  • 447
  • 1
  • 4
  • 5
4
votes
1 answer

Bulk updating a slice of a Python list

I have written a simple implementation of the Sieve of Eratosthenes, and I would like to know if there is a more efficient way to perform one of the steps. def eratosthenes(n): primes = [2] is_prime = [False] + ((n - 1)/2)*[True] for i…
castle-bravo
  • 1,389
  • 15
  • 34
4
votes
3 answers

Generate primes from 1 to n, crashing for n > 300 million

Any suggestions as to how I can get this program to work for n = 1 trillion (aside from making upgrades / buying a new computer)? The error is as follows: after building, the program being executing (the command line style output window pops up)…
iceman
  • 2,020
  • 2
  • 17
  • 24
4
votes
2 answers

How does segmentation improve the running time of Sieve of Eratosthenes?

I came across a segmented implementation of sieve of Eratosthenes which promises to run many times faster than the conventional version. Can someone please explain how segmentation improves the running time? Note that I want to find prime numbers in…
Dhruv Mullick
  • 551
  • 9
  • 25
4
votes
7 answers

Exceeding the size of lists in python

I'm trying to implement the sieve of eratosthenes in python, however when trying to find all primes up to the sqare root of for instance 779695003923747564589111193840021 I get an error saying result of range() has too many items. My question is,…
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