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

Racket streams slower than custom streams?

I implemented a simple and not very efficent sieve of eratosthenes. Once for the built in racket-streams, and once for self defined streams. The only known difference to me is that the built in streams are evaluating the first item on call and not…
Alex
  • 71
  • 4
7
votes
2 answers

Fastest way to prime factorise a number up to 10^18

Given a number 1 <= n <= 10^18, how can I factorise it in least time complexity? There are many posts on the internet addressing how you can find prime factors but none of them (at least from what I've seen) state their benefits, say in a particular…
7
votes
2 answers

Why is this prime sieve implementation slower?

I was just experimenting a bit with (for me) a new programming language: clojure. And I wrote a quite naive 'sieve' implementation, which I then tried to optimise a bit. Strangely enough though (for me at least), the new implementation wasn't…
7
votes
5 answers

Fast algorithm for finding prime numbers?

First of all - I checked a lot in this forum and I haven't found something fast enough. I try to make a function that returns me the prime numbers in a specified range. For example I did this function (in C#) using the sieve of Eratosthenes. I tried…
Ohad
  • 333
  • 2
  • 4
  • 10
7
votes
3 answers

Java implementation of Sieve of Eratosthenes that can go past n = 2^32?

Currently I have this prime generator that is limited to n < 2^32-1. I'm not entirely sure how I could expand the limit further, given the limit of elements in an array. Sieve: public class Main { public static void main(String args[]){ long N…
Arman
  • 655
  • 2
  • 7
  • 23
7
votes
2 answers

Adding wheel factorization to an indefinite sieve

I’m modifying an indefinite sieve of Eratosthenes from here so it uses wheel factorization to skip more composites than its current form of just checking all odds. I’ve worked out how to generate the steps to take to reach all the gaps along the…
7
votes
7 answers

Finding Primes with Modulo in Python

I have been sweating over this piece of code -- which returns all the primes in a list: primes = range(2, 20) for i in range(2, 8): primes = filter(lambda x: x == i or x % i, primes) print primes It works... but I don't understand the role…
fra
  • 205
  • 1
  • 3
  • 7
6
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
  • 567
  • 2
  • 10
  • 25
6
votes
3 answers

Why is this scala prime generation so slow/memory intensive?

I run out of memory while finding the 10,001th prime number. object Euler0007 { def from(n: Int): Stream[Int] = n #:: from(n + 1) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.filter(_ % s.head != 0)) def primes = sieve(from(2)) …
phant0m
  • 16,595
  • 5
  • 50
  • 82
6
votes
1 answer

SPOJ Problem KPRIMES2

I am new to this forum and not well aware of protocols of this forum so pardon me for my ignorance. My question is related to spoj problem https://www.spoj.pl/problems/KPRIMES2/. I am getting TIME LIMIT EXCEED for this problem.I think the bottleneck…
keep_learning
  • 1,057
  • 5
  • 14
6
votes
1 answer

Why doesn't the time complexity of Sieve of Eratosthenes algorithm have the argument sqrt(n)?

I'm trying to understand the Sieve of Eratosthenes algorithm time complexity. Everywhere online, it says the time complexity is O(nloglog(n)), but I don't understand why. Here is some pseudocode factors = new int[n+1]; for i from 2 to n …
user2524624
  • 81
  • 1
  • 7
6
votes
3 answers

Sieve of Eratosthenes in Haskell

I'm solving some classic problems in Haskell to develop my functional skills and I have a problem to implement an optimization suggested at this "Programming Praxis" site: I have three solutions to this problem and the third one is too slow…
6
votes
1 answer

Freezing goal in prolog

I want to freeze my goal until some variable, for example list, is unbounded, right now I have sieve(N,L) :- freeze(Aux,sieve(N,L,[],Aux)), numlist(2,N,Aux). sieve(N,L,R,[H|T]) :- freeze(X, X mod H =\= 0 ; X == H), …
whd
  • 1,819
  • 1
  • 21
  • 52
6
votes
1 answer

In Haskell, what is the most common way to apply a function to every Nth element of a list?

Its very common where I come across some list of elements xs and want to do something to do something with every Nth element. The simplest example would be the Sieve or Erastothenes, where you want to "knock out" every multiple of a given prime. The…
6
votes
3 answers

Find next prime given all prior

I'm writing a recursive infinite prime number generator, and I'm almost sure I can optimize it better. Right now, aside from a lookup table of the first dozen primes, each call to the recursive function receives a list of all previous primes. Since…
1 2
3
31 32