sieve-of-eratosthenes

Is the Sieve of Eratosthenes an example of Dynamic Programming?

半腔热情 提交于 2019-12-08 03:53:08
问题 I'm a bit confused as to whether the Sieve of Eratosthenes (implemented with an array for all the numbers and a loop marking the composite numbers) is an example of Dynamic Programming? A couple of friends were telling me the way it's implemented is an example of Bottom Up DP, but I'm having trouble seeing it. Exactly what are the subproblems and how would you implement SoE with Top-Down / Recursion? Thanks guys. 回答1: Sure, we could think of the Sieve of Eratosthenes as an example of dynamic

Spoj PRIME1 using sieve of eratosthenes (in C)

試著忘記壹切 提交于 2019-12-07 17:13:29
问题 I'm trying to solve the problem PRIME1 using segmented sieve of Eratosthenes. My program works correctly with the normal sieve that is up to NEW_MAX . But there is some problem with cases n > NEW_MAX , where segmented sieving comes into play. In such cases it merely prints all the numbers. Here is the link to the code with relevant test cases: http://ideone.com/8H5lK#view_edit_box /* segmented sieve */ #include <math.h> #include <stdio.h> #include <stdlib.h> #define MAX_LIMIT 1000000000 //10

Getting functional sieve of Eratosthenes fast

岁酱吖の 提交于 2019-12-07 04:57:29
问题 I read this other post about a F# version of this algorithm. I found it very elegant and tried to combine some ideas of the answers. Although I optimized it to make fewer checks (check only numbers around 6) and leave out unnecessary caching, it is still painfully slow. Calculating the 10,000 th prime already take more than 5 minutes. Using the imperative approach, I can test all 31-bit integers in not that much more time. So my question is if I am missing something that makes all this so

Did I just prove that sieve of Eratosthenes is less efficient than trial division?

谁说我不能喝 提交于 2019-12-07 02:17:02
问题 I was trying to compare the run-time speed of two algorithms: A brute-force C program to print prime numbers (10,000 numbers), and a Sieve of Eratosthenes C program (also 10,000 prime numbers). My measured run-time for the sieve algorithm was: 0.744 seconds My measured run-time for the brute-force algorithm was: 0.262 seconds However, I was told that the Sieve of Eratosthenes algorithm is more efficient than the brute-force method, and so I thought it would run faster . So either I'm wrong or

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

牧云@^-^@ 提交于 2019-12-07 01:43:55
问题 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 = 2000000000; // initially assume all integers are prime boolean[] isPrime = new boolean[N + 1]; for (int i = 2; i <= N; i++) { isPrime[i] = true; } // mark non-primes <= N using Sieve of Eratosthenes for (int i = 2; i*i <= N; i++) { // if i is prime,

Sieve of Eratosthenes has huge 'overdraw' - is Sundaram's better after all?

北城余情 提交于 2019-12-06 06:22:26
问题 The standard Sieve of Eratosthenes crosses out most composites multiple times; in fact the only ones that do not get marked more than once are those that are the product of exactly two primes. Naturally, the overdraw increases as the sieve gets bigger. For an odd sieve (i.e. without the evens) the overdraw hits 100% for n = 3,509,227, with 1,503,868 composites and 1,503,868 crossings-out of already crossed-out numbers. For n = 2^32 the overdraw rises to 134.25% (overdraw 2,610,022,328 vs. pop

Clojure: Avoiding stack overflow in Sieve of Erathosthene?

南笙酒味 提交于 2019-12-05 18:19:12
问题 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 (sieve (nats-from 2))) Now, it's all OK when i take first 100 primes: (take 100 primes) But, if i try to take first 1000 primes , program breaks because of stack overflow. I'm wondering if is it

Why does function apply complain about long lists?

柔情痞子 提交于 2019-12-05 13:24:53
As part of some Eulerian travails , I'm trying to code a Sieve of Eratosthenes with a factorization wheel. My code so far is: (defun ring (&rest content) "Returns a circular list containing the elements in content. The returned list starts with the first element of content." (setf (cdr (last content)) content)) (defun factorization-wheel (lst) "Returns a circular list containing a factorization wheel using the list of prime numbers in lst" (let ((circumference (apply #'* lst))) (loop for i from 1 to circumference unless (some #'(lambda (x) (zerop (mod i x))) lst) collect i into wheel finally

Did I just prove that sieve of Eratosthenes is less efficient than trial division?

人走茶凉 提交于 2019-12-05 08:36:00
I was trying to compare the run-time speed of two algorithms: A brute-force C program to print prime numbers (10,000 numbers), and a Sieve of Eratosthenes C program (also 10,000 prime numbers). My measured run-time for the sieve algorithm was: 0.744 seconds My measured run-time for the brute-force algorithm was: 0.262 seconds However, I was told that the Sieve of Eratosthenes algorithm is more efficient than the brute-force method, and so I thought it would run faster . So either I'm wrong or my program is flawed (which I doubt). Therefore, my question is: Since I got the opposite results than

CUDA - Sieve of Eratosthenes division into parts

空扰寡人 提交于 2019-12-05 05:42:57
问题 I'm writing implementation of Sieve of Eratosthenes (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) on GPU. But no sth like this - http://developer-resource.blogspot.com/2008/07/cuda-sieve-of-eratosthenes.html Method: Creating n-element array with default values 0/1 (0 - prime, 1 - no) and passing it on GPU (I know that it can be done directly in kernel but it's not problem in this moment). Each thread in block checks multiples of a single number. Each block checks in total sqrt(n)