sieve

SPOJ PRIME1 : TLE [closed]

假如想象 提交于 2019-12-11 01:27:57
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I tried implementing the segmented sieve algorithm for this [question]:http://www.spoj.pl/problems/PRIME1/ as follows : #include <iostream> #include <string> #include <set> #include<math.h> #include<vector> #include<cstdlib> #include<cstdio> #include<cstring> #include<cstdio> #define MAX 32000 // sqrt of the

Prime Number generator with recursion and list comprehension

这一生的挚爱 提交于 2019-12-10 10:22:50
问题 I am a newbie to Haskell programming and have trouble understanding how the below list comprehension expands. primes = sieve [2..] sieve (p:xs) = p : sieve [x | x <-xs, x `mod` p /= 0] Can someone correct me how the sieve expansion works: As we are pattern matching in sieve , p would associate to 2 and x s from [3..] . Next, in the list comprehension x<-3 , but then how can we call sieve with just 3 when there is no short-circuit. Other thing I do not understand is how recursion works here. I

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

Fastest prime test for small-ish numbers

浪尽此生 提交于 2019-12-06 00:30:19
问题 I'm playing through project Euler in my spare time, and it's come to the point where I need to do some refactoring. I've implemented Miller-Rabin, as well as a few sieves. I've heard before that sieves are actually faster for small-ish numbers, as in under a few million. Does anybody have any information on this? Google wasn't very helpful. 回答1: Yes, you'll find with most algorithms that you can trade space for time. In other words, by allowing the use of more memory, the speed is greatly

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

蹲街弑〆低调 提交于 2019-12-04 12:38:22
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 count 1,944,203,427 = (2^32 / 2) - 203,280,221). The Sieve of Sundaram - with another explanation at

2-3-5-7 wheel factorization seems to skip prime number 331

这一生的挚爱 提交于 2019-12-04 12:37:00
When following the procedure on wikipedia for wheel factorization , I seem to have stumbled into a problem where the prime number 331 is treated as a composite number if I try to build a 2-3-5-7 wheel. With 2-3-5-7 wheel, 2*3*5*7=210. So I setup a circle with 210 slots and go through steps 1-7 without any issues. Then I get to step 8 and strike off the spokes of all multiples of prime numbers, I eventually strike off the spoke rooted at 121, which is a multiple of 11, which is a prime. For the spoke rooted at 121, 121 + 210 = 331. Unfortunately, 331 is a prime number. Is the procedure on

How do I generate Primes Using 6*k +- 1 rule

故事扮演 提交于 2019-12-03 11:04:47
问题 We know that all primes above 3 can be generated using: 6 * k + 1 6 * k - 1 However we all numbers generated from the above formulas are not prime. For Example: 6 * 6 - 1 = 35 which is clearly divisible by 5. To Eliminate such conditions, I used a Sieve Method and removing the numbers which are factors of the numbers generated from the above formula. Using the facts: A number is said to be prime if it has no prime factors. As we can generate all the prime numbers using the above formulas. If

How do I generate Primes Using 6*k +- 1 rule

梦想与她 提交于 2019-12-03 02:38:40
We know that all primes above 3 can be generated using: 6 * k + 1 6 * k - 1 However we all numbers generated from the above formulas are not prime. For Example: 6 * 6 - 1 = 35 which is clearly divisible by 5. To Eliminate such conditions, I used a Sieve Method and removing the numbers which are factors of the numbers generated from the above formula. Using the facts: A number is said to be prime if it has no prime factors. As we can generate all the prime numbers using the above formulas. If we can remove all the multiples of the above numbers we are left with only prime numbers. To generate

Count of divisors of numbers till N in O(N)?

无人久伴 提交于 2019-12-01 11:29:18
So, we can count divisors of each number from 1 to N in O(NlogN) algorithm with sieve: int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) { cnt[j]++; //// here cnt[x] means count of divisors of x } } Is there way to reduce it to O(N)? Thanks in advance. Here is a simple optimization on @גלעד ברקן's solution. Rather than use sets, use arrays. This is about 10x as fast as the set version. n = 100 answer = [None for i in range(0, n+1)] answer[1] = 1 small_factors = [1] p = 1 while (p < n): p = p + 1 if answer[p] is None: print("\n\nPrime: " + str(p)) limit = n / p new

Count of divisors of numbers till N in O(N)?

让人想犯罪 __ 提交于 2019-12-01 09:03:28
问题 So, we can count divisors of each number from 1 to N in O(NlogN) algorithm with sieve: int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) { cnt[j]++; //// here cnt[x] means count of divisors of x } } Is there way to reduce it to O(N)? Thanks in advance. 回答1: Here is a simple optimization on @גלעד ברקן's solution. Rather than use sets, use arrays. This is about 10x as fast as the set version. n = 100 answer = [None for i in range(0, n+1)] answer[1] = 1 small