sieve-of-eratosthenes

Sieve of Eratosthenes in Ruby

Rather than scraping a Ruby version of this algorithm off the net I wanted to create my own based on its description here. However I cannot figure out two things def primeSieve(n) primes = Array.new for i in 0..n-2 primes[i] = i+2 end index = 0 while Math.sqrt(primes.last).ceil > primes[index] (primes[index] ** 2).ste...

Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Eratosthenes method, implemented it, and changed the question title and tags appropriately. Thanks to everyone who helped! Introduction I wrote this fancy little method that generates...

Sieve of Atkin explanation

I am doing a project at the moment and I need an efficient method for calculating prime numbers. I have used the sieve of Eratosthenes but, I have been searching around and have found that the sieve of Atkin is a more efficient method. I have found it difficult to find an explanation (that I have been able to understand!) of this method....

Sieve of Eratosthenes problem: handling really big numbers.

I'm solving Sphere's Online Judge Prime Generator using the Sieve of Eratosthenes. My code works for the test case provided. But.. as the problem clearly states: The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-...

Time complexity of the program

#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf("%ld",&n); n=100000; j=2; start=clock(); printf("\n%ld",j); for(j=3;j<=n;j+=2) { for(i=3;i*i<=j;i+=2) if(j%i==0) break; if(i*i>j) prin...

Program to find prime numbers

I'm trying to construct a program that finds prime numbers between zero and the number input (a really long number); but the program isn't returning an output like I'm expecting it to: Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication16 { class Program { ...

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 (make-vector (+ 1 max-index) #t))) (let loop ((i 0) (ps '(2))) (let ((p (+ i i 3)) (startj...

Sieve of Eratosthenes Algorithm

Hi All, I did some searching and was not able to find any information regarding this implementation versus every other one I have seen. function sieve($top) { for($i = 11; $i<$top; $i+=2) { if($i % 3 == 0 || $i % 5 == 0 || $i % 7 == 0) { continue; } echo "$i <br /...

Why do I fail Project Euler #10?

Question is: Find the sum of all the primes below 2 million. I pretty much did the Sieve of Erastothenes thing, and the program below seems to work for small number i.e. define LIMIT as 10L produces 17 as answer. I submitted 1179908154 as the answer, as produced by the following program, and it was incorrect. Please help pointing out ...

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, how do I avoid this problem, if I instantiate the list with a while loop I will get an error...

Time complexity of Sieve of Eratosthenes algorithm

From Wikipedia: The complexity of the algorithm is O(n(logn)(loglogn)) bit operations. How do you arrive at that? That the complexity includes the loglogn term tells me that there is a sqrt(n) somewhere. Suppose I am running the sieve on the first 100 numbers (n = 100), assuming that marking the numbers as c...

Speed up bitstring/bit operations in Python?

I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def prime_numbers(limit=1000000): '''Prime number generator. Yields the series 2, 3, 5, 7, 11, 13, 17, 19,...

Clojure: Avoiding stack overflow in Sieve of Erathosthene?

Here's my implementation of Sieve of Erathosthene in Clojure (based on SICP lesson on streams): (defn nats-from [n] (iterate inc n)) (defn divide? [p q] (zero? (rem q p))) (defn sieve [stream] (lazy-seq (cons (first stream) (sieve (remove #(divide? (first stream) %) (rest stream)))))) (def primes (si...

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 also Atkin's sieve but the Eratosthenes one runs faster (in my implementation): public s...

My Sieve of Eratosthenes takes too long

I have implemented "sieve of eratosthenes" to solve an SPOJ problem. Though the output is fine, my submission exceeds the time limit. How can I reduce the run time? int main() { vector<int> prime_list; prime_list.push_back(2); vector<int>::iterator c; bool flag=true; unsigned int m,n; for(int i=3; i<=32000;i+=2) { flag...

Sieve of Eratosthenes - Finding Primes Python

Just to clarify, this is not a homework problem :) I wanted to find primes for a math application I am building & came across Sieve of Eratosthenes approach. I have written an implementation of it in Python. But it's terribly slow. For say, if I want to find all primes less than 2 million. It takes > 20 mins. (I stopped it at this poi...