sieve-of-eratosthenes

Sieve of Eratosthenes Algorithm

江枫思渺然 提交于 2020-01-06 14:17:11
问题 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 />"; } } Yeah I know it just prints it out, but that's not the important part. What is the major pitfall whether it be time or other? EDIT: Are there any other issues beyond scalability? Also again thanks for the comments about moving forward

Clojure - tail recursive sieve of Eratosthenes

旧城冷巷雨未停 提交于 2019-12-30 08:23:34
问题 I have this implementation of the sieve of Eratosthenes in Clojure: (defn sieve [n] (loop [last-tried 2 sift (range 2 (inc n))] (if (or (nil? last-tried) (> last-tried n)) sift (let [filtered (filter #(or (= % last-tried) (< 0 (rem % last-tried))) sift)] (let [next-to-try (first (filter #(> % last-tried) filtered))] (recur next-to-try filtered)))))) For larger n (like 20000) it ends with stack overflow. Why doesn't tail call elimination work here? How to fix it? 回答1: Problem: filter does lazy

Fast algorithm for finding prime numbers? [duplicate]

拥有回忆 提交于 2019-12-30 06:26:12
问题 This question already has answers here : Which is the fastest algorithm to find prime numbers? (14 answers) Closed 6 years ago . 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 static void SetPrimesSieve(int

Fast algorithm for finding prime numbers? [duplicate]

孤街浪徒 提交于 2019-12-30 06:25:31
问题 This question already has answers here : Which is the fastest algorithm to find prime numbers? (14 answers) Closed 6 years ago . 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 static void SetPrimesSieve(int

What's the ideal implementation for the Sieve of Eratosthenes between Lists, Arrays, and Mutable Arrays?

社会主义新天地 提交于 2019-12-30 04:45:18
问题 In Haskell, I've found three simple implementations of the Sieve of Eratosthenes on the Rosetta Code page. Now my question is, which one should be used in which situations? Correcting my initial reasoning would be helpful too: I'm assuming the List one is the most idiomatic and easy to read for a Haskeller. Is it correct, though? I'm wondering if it suffers from the same problems as another list-based sieve that I then learned was not actually implementing the algorithm: (edit: shown here is

Sieve of Eratosthenes Scheme

岁酱吖の 提交于 2019-12-29 08:05:42
问题 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

Segmented Sieve of Erastothenes C++ SPOJ [duplicate]

我只是一个虾纸丫 提交于 2019-12-26 03:09:05
问题 This question already has answers here : Segmented Sieve of Eratosthenes? (6 answers) Efficient algorithm to get primes between two large numbers (10 answers) Closed 5 years ago . I'm aware this has been asked before, but I cannot fully understand how to implement Segmented Sieve of Eratosthenes. Problem 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-m<=100000) separated by a

is Sieve of erathosthens the best algorithm to generate prime numbers from 1 to N?

纵饮孤独 提交于 2019-12-23 15:53:14
问题 I was asked this question in an interview. I implemented an algorithm using sieve of eratosthenes concept and an array. Is there a better way to go about this question For those who dont know the sieve , here is the link: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes EDIT: Best in terms of both time and space complexity. I just told them the flaw of SoE is space complexity. So they asked me if I could do something about it . Here is how the interview went about: 1) Implement a algo that

An Efficient Sieve of Eratosthenes in Python

雨燕双飞 提交于 2019-12-23 13:54:32
问题 This very short and simple code in #Python tries to simulate the "Sieve of Eratosthenes" for the first N natural numbers with the constraints of (0) script shortness; (1) minimization of the 'if statements' and 'for/while loops'; (2) efficiency in terms of CPU time. import numpy as np N = 10**5 a = np.array(range(3,N,2)) for j in range(0, int(round(np.sqrt(N),0))): a[(a!=a[j]) & (a%a[j] == 0)] = 0 a = a[a!=0] a = [2]+list(a) On an Intel Core I5, it returns the prime numbers among the first: N

An Efficient Sieve of Eratosthenes in Python

喜你入骨 提交于 2019-12-23 13:54:08
问题 This very short and simple code in #Python tries to simulate the "Sieve of Eratosthenes" for the first N natural numbers with the constraints of (0) script shortness; (1) minimization of the 'if statements' and 'for/while loops'; (2) efficiency in terms of CPU time. import numpy as np N = 10**5 a = np.array(range(3,N,2)) for j in range(0, int(round(np.sqrt(N),0))): a[(a!=a[j]) & (a%a[j] == 0)] = 0 a = a[a!=0] a = [2]+list(a) On an Intel Core I5, it returns the prime numbers among the first: N