sieve-of-eratosthenes

Sieve of Eratosthenes

无人久伴 提交于 2019-11-28 05:07:33
问题 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;

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

我是研究僧i 提交于 2019-11-27 15:14:19
问题 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

Sieve of Eratosthenes in Ruby

不打扰是莪最后的温柔 提交于 2019-11-27 13:55:20
问题 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

Sieve of Eratosthenes algorithm in JavaScript running endless for large number

拥有回忆 提交于 2019-11-27 11:57:54
I have been trying to write Sieve of Eratosthenes algorithm in JavaScript. Basically I just literally followed the steps below: Create a list of consecutive integers from 2 to (n-1) Let first prime number p equal 2 Starting from p, count up in increments of p and removes each of these numbers (p and multiples of p) Go to the next number in the list and repeat 2,3,4 Add unintentionally deleted prime numbers back to the list and this is what I have come up with: function eratosthenes(n){ var array = []; var tmpArray = []; // for containing unintentionally deleted elements like 2,3,5,7,... var

Sieve of Eratosthenes algorithm in C

前提是你 提交于 2019-11-27 06:31:22
问题 Okay, so this function I created uses the Sieve of Eratosthenes algorithm to compute all the primes <= n. This function stores the prime numbers and the count of primes in the parameters. When the function exits, primes should be pointing to a chunk of dynamically allocated memory that holds all the primes <= num. *count will have the count of primes. Here is my function getPrimes : void getPrimes(int num, int* count, int** array){ (*count) = (num - 1); int sieve[num-1], primenums = 0, index,

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

雨燕双飞 提交于 2019-11-27 06:29:38
问题 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 ? 回答1: If you have enough space to store all the primes up to sqrt(b) then you

Implementing the Page Segmented Sieve of Eratosthenes in Javascript

↘锁芯ラ 提交于 2019-11-27 06:29:25
问题 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

A Fast Prime Number Sieve in Python

╄→гoц情女王★ 提交于 2019-11-27 06:14:33
问题 I have been going through prime number generation in python using the sieve of Eratosthenes and the solutions which people tout as a relatively fast option such as those in a few of the answers to a question on optimising prime number generation in python are not straightforward and the simple implementation which I have here rivals them in efficiency. My implementation is given below def sieve_for_primes_to(n): size = n//2 sieve = [1]*size limit = int(n**0.5) for i in range(1,limit): if

The sieve of Eratosthenes in F#

折月煮酒 提交于 2019-11-27 03:13:16
I am interested in an implementation of the sieve of eratosthenes in purely functional F#. I am interested in an implementation of the actual sieve, not the naive functional implementation that isn't really the sieve , so not something like this: let rec PseudoSieve list = match list with | hd::tl -> hd :: (PseudoSieve <| List.filter (fun x -> x % hd <> 0) tl) | [] -> [] The second link above briefly describes an algorithm that would require the use of a multimap, which isn't available in F# as far as I know. The Haskell implementation given uses a map that supports an insertWith method, which

Adding wheel factorization to an indefinite sieve

只谈情不闲聊 提交于 2019-11-27 02:21:38
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 wheel. From there I figured I could just substitute the +2’s for these wheel steps but it’s causing the sieve to skip primes. Here's the code: from itertools import count, cycle def dvprm(end): "finds primes by trial division. returns a list" primes=[2] for i in range(3, end+1, 2): if all(map(lambda x:i%x, primes)): primes.append(i) return primes def