sieve-of-eratosthenes

Why is this prime test so slow?

孤街浪徒 提交于 2019-11-30 02:40:22
问题 This code was taken from the book "Haskell Road to Logic, Math and Programming". It implements sieve of eratosthenes algorithm and solves Project Euler Problem 10. sieve :: [Integer] -> [Integer] sieve (0 : xs) = sieve xs sieve (n : xs) = n : sieve (mark xs 1 n) where mark :: [Integer] -> Integer -> Integer -> [Integer] mark (y:ys) k m | k == m = 0 : (mark ys 1 m) | otherwise = y : (mark ys (k+1) m) primes :: [Integer] primes = sieve [2..] -- Project Euler #10 main = print $ sum $ takeWhile (

Sieve of Atkin explanation

耗尽温柔 提交于 2019-11-30 01:45:36
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. How does it work? Example code (preferably in C or python) would be brilliant. Edit: thanks for your help, the only thing that I still do not understand is what the x and y variables are referring to in the pseudo code. Could someone please shed some light on

Sieve of Eratosthenes implementation

南笙酒味 提交于 2019-11-29 18:02:01
I am trying to implement algorithm for Sieve of Eratosthenes but I don't know why this program crashes for larger programs. Initially I was using vector but now I am implementing this using dynamic memory allocation. #include<iostream> #include<cmath> #include<cstdlib> using namespace std; unsigned isqrt(unsigned value) { return static_cast<unsigned>(sqrt(static_cast<float>(value))); } int main() { int t; cin >> t; long long * N; long long * M; long long n, m; N = new long long[t]; M = new long long[t]; for(int i = 0; i < t ; i++){ cin >> M[i] >> N[i]; } for(int i = 0; i < t ; i++){ n = N[i];

Sieve of Eratosthenes

淺唱寂寞╮ 提交于 2019-11-29 12:09:40
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 same implementation for 2 million it's giving me a segmentation fault... I have a certain idea of why the error is coming but don't know how to correct it... Here's the code for primes under 1 million. #include<stdio.h> int main(void) { int i,k=2; int j; int n=1000000; int prime[2000000]={}; for(i=0;i<n;i++) // initializes the prime number array {

Sieve of Eratosthenes Scheme

一个人想着一个人 提交于 2019-11-29 11:45:16
I've been searching the web for an implementation of the Sieve of Eratosthenes in scheme and although I came up with a lot of content, none of them seemed to have made it like I need it to be done. The problem is most algorithms either use a static end or use iteration. This paired with my lack of knowledge of the language led me to ask all of you for help. I need an implementation of the Sieve that takes in one argument (number to Sieve until), uses only recursion and has a list of "cons" of a number with a #t (true) or #f (false). So essentially the algorithm would go as such: Make list from

Implementing the Page Segmented Sieve of Eratosthenes in Javascript

牧云@^-^@ 提交于 2019-11-29 08:43:39
I recently read about a faster implementation of Segmented Sieve of Eratosthenes for really big numbers. Following is an implementation of the same: function sieve(low, high) { var primeArray = [], ll = Math.sqrt(low), output = []; for (var i = 0; i < high; i++) { primeArray[i] = true; } for (var i = 2; i <= ll; i++) { if (primeArray[i]) { for (var j = i * i; j < high; j += i) { primeArray[j] = false; } } } for (var i = 2; i < ll; i++) { if(primeArray[i]) { var segmentStart = Math.floor(low/i) * i; for(var j = segmentStart; j <= high; j+=i) { primeArray[j] = false; } } } for(var i = low; i <=

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

房东的猫 提交于 2019-11-29 00:14:51
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)) def main(args: Array[String]): Unit = { println(primes(10001)) } } Is this because after each "iteration" (is this the correct term in this context?) of primes , I increase the stack of functions to be called to get the next element by one? One solution that I've found on the web which doesn't resort to an iterative solution (which I'd like to avoid to

Sieve of Eratosthenes in Ruby

﹥>﹥吖頭↗ 提交于 2019-11-28 21:31:47
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).step(primes.length - 1, primes[index]) {|x| x % primes[index] == 0 ? primes.delete(x) : ""} index += 1 end primes end Why it doesn't iterate to the end of the array? According to the description in the link above the loop should be broken out of when the squareroot of the last

Sieve of Eratosthenes implementation

我们两清 提交于 2019-11-28 12:38:37
问题 I am trying to implement algorithm for Sieve of Eratosthenes but I don't know why this program crashes for larger programs. Initially I was using vector but now I am implementing this using dynamic memory allocation. #include<iostream> #include<cmath> #include<cstdlib> using namespace std; unsigned isqrt(unsigned value) { return static_cast<unsigned>(sqrt(static_cast<float>(value))); } int main() { int t; cin >> t; long long * N; long long * M; long long n, m; N = new long long[t]; M = new

How do i reduce the space complexity in Sieve of Eratosthenes to generate prime between a and b?

こ雲淡風輕ζ 提交于 2019-11-28 11:47:54
After getting through some of the SO posts , i found Sieve of Eratosthenes is the best & fastest way of generating prime numbers. I want to generate the prime numbers between two numbers, say a and b . AFAIK, in Sieve's method, the space complexity is O( b ). PS: I wrote Big-O and not Theta, because i don't know whether the space requirement can be reduced. Can we reduce the space complexity in Sieve of Eratosthenes ? If you have enough space to store all the primes up to sqrt(b) then you can sieve for the primes in the range a to b using additional space O(b-a). In Python this might look like