sieve-of-eratosthenes

Finding the 10001st prime number (in python)? [duplicate]

别来无恙 提交于 2019-12-12 03:24:56
问题 This question already has answers here : Fastest way to list all primes below N (34 answers) Closed 3 years ago . I'm currently trying to use an implementation of the sieve of erasthonese, but it still takes a very long time to find a long list of prime numbers. def sieve(n=1000000): not_prime = [] prime = [] for i in range(2, n+1): if i not in not_prime: prime.append(i) for j in range(i*i, n+1, i): not_prime.append(j) return prime[10002] I tried to hard code to what value the sieve should

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

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

Merged iterators produce obscure results

不想你离开。 提交于 2019-12-11 03:47:44
问题 I'm trying to implement prime number generator using Sieve of Eratosthenes algorithm. I do it just to try using recursive iterator merging to implement sifter. What I do is this: from itertools import count,islice,groupby from heapq import merge def primes3(): p = 2 yield p sifter = (i*p for i in count(p)) s = next(sifter) for p in count(p+1): if p==s: # this p is sieved out print('s: {}'.format(s)) s = next(sifter) else: yield p # this is prime print('p: {}'.format(p)) sifter = (k for k, g

runtime error (NZEC) Java SPOJ

自古美人都是妖i 提交于 2019-12-11 02:48:21
问题 I'm trying to do a SPOJ problem called Prime Generator. Although the output works on my computer it doesn't work when I try to run it on SPOJ. The following error message occurs. Error: runtime error (NZEC) Can you help me find what it is? import java.util.BitSet; import java.util.Scanner; class Prime_generator { public static void main(String[] args) { Scanner input=new Scanner(System.in); int number_of_entries=input.nextInt(); int [] entries=new int[number_of_entries*2]; for(int i=0;i

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

C++ Array Allocation Segmentation Fault 11 Newbie

心不动则不痛 提交于 2019-12-10 11:45:04
问题 I am learning C++ from Algorithms in C++ by Robert Sedgewick. Right now I am working on the Sieve of Eratosthenes with a user specified upper bound on the largest prime. When I run the code with max 46349, it runs and prints out all primes up to 46349, however when I run the code with max 46350, a Segmentation fault occurs. Can someone help to explain why? ./sieve.exe 46349 2 3 5 7 11 13 17 19 23 29 31 ... ./sieve.exe 46350 Segmentation fault: 11 Code: #include<iostream> using namespace std;

Linear time Euler's Totient Function calculation

房东的猫 提交于 2019-12-09 23:41:14
问题 After surfing a bit ,I found this code for calculating Euler's phi values in linear time using Sieve of Eratostenes .But failed to understand the main logic used in this code,specially what is done in the inner for loop and idea used in this loop for calculating phi value.It will be helpful if someone helps me understand this code. #define MAXN 3000000 int phi[MAXN + 1], prime[MAXN/10], sz; bitset <MAXN + 1> mark; for (int i = 2; i <= MAXN; i++ ){ if(!mark[i]){ phi[i] = i-1; prime[sz++]= i; }

Prime Sieve in Haskell

眉间皱痕 提交于 2019-12-09 05:39:16
问题 I'm very new to Haskell and I'm just trying to find the sum of the first 2 million primes. I'm trying to generate the primes using a sieve (I think the sieve of Eratosthenes?), but it's really really slow and I don't know why. Here is my code. sieve (x:xs) = x:(sieve $ filter (\a -> a `mod` x /= 0) xs) ans = sum $ takeWhile (<2000000) (sieve [2..]) Thanks in advance. 回答1: It is very slow because the algorithm is a trial division that doesn't stop at the square root. If you look closely what

Sum of primes below two million. Sieve of Eratosthenes

倖福魔咒の 提交于 2019-12-08 07:00:32
问题 Having a little trouble solving Problem: 'Calculate the sum of primes below two million'. I'm using the 'Sieve of Eratosthenes' method. My method works fine for finding primes till hundred but when I try to find the sum of primes till 2,000,000 I get an incorrect answer. #include <iostream> using namespace std; long long unsigned int number[2000008]; int x=2000000LLU; int sum() { int s=0LLU; //stores sum for(int y=2; y<=x; y++) //add all the numers in the array from 2 to 2 million { s+=number