sieve

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

家住魔仙堡 提交于 2020-01-01 15:07:13
问题 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

Sieve Of Atkin is surprisingly slow

主宰稳场 提交于 2019-12-25 02:28:52
问题 I recently became very interested in prime numbers and tried making programs to calculate them. I was able to make a sieve of Sundaram program that was able to calculate a million prime numbers in a couple seconds. I believe that's pretty fast, but I wanted better. I went on to try to make a Sieve of Atkin, I slapped together working C++ code in 20 minutes after copying the pseudocode from Wikipedia. I knew that it wouldn't be perfect because after all, its pseudocode. I was expecting at

Finding composite numbers

僤鯓⒐⒋嵵緔 提交于 2019-12-24 12:39:02
问题 I have a range of random numbers. The range is actually determined by the user but it will be up to 1000 integers. They are placed in this: vector<int> n and the values are inserted like this: srand(1); for (i = 0; i < n; i++) v[i] = rand() % n; I'm creating a separate function to find all the non-prime values. Here is what I have now, but I know it's completely wrong as I get both prime and composite in the series. void sieve(vector<int> v, int n) { int i,j; for(i = 2; i <= n; i++) { cout <<

Sieve of Eratosthenes - Implementation returning some non-prime values?

旧街凉风 提交于 2019-12-24 03:42:31
问题 I implemented the Sieve of Eratosthenes in Java, from pseudocode: public static void sieveofEratosthenes(int n) { boolean numArray[]; numArray = new boolean[n]; for(int i = 0; i < n; i++) numArray[i] = true; int a = 0; for(int i = 2; i < Math.sqrt((double)n); i++) { if(numArray[i]) { for(int j = (int)Math.pow(i, 2); j < n; a++) { numArray[j] = false; j += (a * i); } } } for(int i = 2; i < n; i++) { if(numArray[i]) System.out.println(i); } } The output it gives me, when i is 15: 2 3 5 7 8 11

Sieve of Eratosthenes - Implementation returning some non-prime values?

こ雲淡風輕ζ 提交于 2019-12-24 03:42:03
问题 I implemented the Sieve of Eratosthenes in Java, from pseudocode: public static void sieveofEratosthenes(int n) { boolean numArray[]; numArray = new boolean[n]; for(int i = 0; i < n; i++) numArray[i] = true; int a = 0; for(int i = 2; i < Math.sqrt((double)n); i++) { if(numArray[i]) { for(int j = (int)Math.pow(i, 2); j < n; a++) { numArray[j] = false; j += (a * i); } } } for(int i = 2; i < n; i++) { if(numArray[i]) System.out.println(i); } } The output it gives me, when i is 15: 2 3 5 7 8 11

Is there a way to find the approximate value of the nth prime?

两盒软妹~` 提交于 2019-12-17 23:32:43
问题 Is there a function which will return the approximate value of the n th prime? I think this would be something like an approximate inverse prime counting function. For example, if I gave this function 25 it would return a number around 100, or if I gave this function 1000 it would return a number around 8000. I don't care if the number returned is prime or not, but I do want it to be fast (so no generating the first n prime numbers to return the n th.) I would like this so that I can generate

integer division vs regular division

坚强是说给别人听的谎言 提交于 2019-12-12 02:18:26
问题 I need to compare an integer in a loop to the quotient of a long and a long . in order to not do integer division, if I understand correctly do I need to convert one of the longs into a double? long prime = primes[d]; int i = 1; // "inputNumber / prime" should not be integer division, while it is now. // How do I do this yet still compare it to an "int" afterwards? while (i < (inputNumber / prime)) { primes[i*prime] = 0; i++; } That's the code snippet. primes is an array filled with long s.

Is this an optimal prime generator?

隐身守侯 提交于 2019-12-11 10:16:46
问题 Is this in any way an optimal solution for finding primes? I am not trying to add every optimization under the sun, but is the principal good? def primesUpto(self, x): primes = [2] sieve = [2] i = 3 while i <= x: composite = False j = 0 while j < len(sieve): sieve[j] = sieve[j] - 1 if sieve[j] == 0: composite = True sieve[j] = primes[j] j += 1 if not composite: primes.append(i) sieve.append(i*i-i) i += 1 return primes 回答1: Hmm, very interesting. Your code is actual honest to goodness genuine

How do I define the sieve function for prime computation using higher–order functions?

两盒软妹~` 提交于 2019-12-11 05:11:58
问题 I have a recursive definition of sieve in Haskell for prime number computation. But I don’t know how to write the same function using higher–order functions such as map or filter . Can anybody show me please? sieve [] = [] sieve (x:xs) = check (x:xs) check [] = [] check (x:xs) |x/=2 && x/=3 && x/=5 && x/=7 = comp (x:xs) |otherwise = x : sieve xs comp [] = [] comp (x:xs) |x `mod` 2 == 0 = sieve xs |x `mod` 3 == 0 = sieve xs |x `mod` 5 == 0 = sieve xs |x `mod` 7 == 0 = sieve xs |otherwise = x :

Sieve of Erastothenes

一笑奈何 提交于 2019-12-11 04:28:57
问题 I'm trying to figure out how to use the sieve of eratosthenes to find the prime numbers from 1-300. I'm having trouble figuring it out, so any help would be nice! Btw, im new to programming so if you could keep it simple that would be best Below is my code (so far) #include <stdio.h> #include <simpio.h> #include <genlib.h> #include <math.h> #define max 301 main() { bool is_prime[max]; int i, int1, j, n; int1=sqrt(max); for(n=0; n<=max; n++); { is_prime[n]=TRUE; //set everything to prime } is