Questions tagged [sieve-of-eratosthenes]

Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer.

Sieve of Eratosthenes finds primes among the natural numbers above 1 between the composites, which it finds by direct enumeration from each prime (optimization: prime's square), as an arithmetic progression with the step equal to that prime.

The first prime number is 2.

In pseudocode, the timing issues aside, it is

primes = [2, 3, ...] \ [[p*p, p*p+p, ...] for p in primes]

469 questions
0
votes
1 answer

Whats the purpose of using n+1 in this eratosthenes function?

Here is a simple eratosthenes sieve for prime numbers, which removes the multiples and append them in the empty list of multiples. My question is that if I use n instead of n+1 in both for loops the answer comes out the same. def eratosthenes(n): …
0
votes
3 answers

Sieve of Eratosthenes in Python

I am trying to write a python function to return the number of primes less than a given value and the values of all the primes. I need to use the Sieve of Eratosthenes algorithm. I believe I'm missing something in the function - For example, when I…
user2203774
  • 609
  • 4
  • 13
  • 25
0
votes
4 answers

Is this more efficient than Sieve of Eratosthenes?

I wrote a code in Python 2.7 for creating list of prime numbers. The code is def primes_list(num): ans = [2] for i in range(3, num, 2): for j in ans: if i % j == 0: break else: …
Aseem Bansal
  • 6,722
  • 13
  • 46
  • 84
0
votes
2 answers

How to write a function that lists prime numbers using the sieve of Eratosthenes

I am supposed to code a function or script that finds all prime numbers p smaller than a given integer n>2 using the "sieve of Eratosthenes" avoiding unecessary storage(I can create a vector of length n but not more) n = input('Enter your…
Nasibabuba
  • 179
  • 1
  • 2
  • 6
0
votes
1 answer

Sieve of Eratosthenes using a bit array

I have a bit array prime[]of unsigned int. I wish to implement a Sieve of Eratosthenes using this array, by having each bit represent a number n. That is, given n, the array element that holds the bit that corresponds to n would be prime[n/32] and…
yiwei
  • 4,022
  • 9
  • 36
  • 54
0
votes
4 answers

Sieve of Eratosthenes implementation

I am trying to implement algorithm for Sieve of Eratosthenes but I don't know why this program crashes for larger programs. Initially I was using vector but now I am implementing this using dynamic memory…
Sumit Gera
  • 1,249
  • 4
  • 18
  • 34
0
votes
2 answers

Weird Output with first case integer

Here are two functions below that compile perfectly but I seem to be getting a weird error with the very first inputted integer. I have tried debugging in GDB but when it's only the first inputted value that is having this weird error, then it makes…
0
votes
1 answer

Sieve of Eratosthenes near complexity approximation

I have tried to give a more precise approximation to Sieve of Eratosthenes. Elementary operations and weights that I used: prime[p] -> 1 operation m = p * p -> 2 operations prime[m] = false -> 1 operation m = m + p -> 2…
flatronka
  • 1,061
  • 25
  • 51
0
votes
1 answer

How to Optimize CUDA Sieve of Eratosthenes

I'm new to CUDA. To get my hands dirty, I tried writing a Sieve of Eratosthenes (for finding all the primes up to some number n). There are a number of things I had to do to get it to work that it seems shouldn't have been necessary. I'm curious…
dspyz
  • 5,280
  • 2
  • 25
  • 63
0
votes
0 answers

Multithreading Sieve of Eratosthenes - Taking a very very long time

I am attempting to create a multi-threaded Sieve of Eratosthenes The number of threads are set by default to 4, though they can be specified by the user as a command line argument. I'm attempting to mark all intervals of the prime within the array…
0
votes
3 answers

Sieve of Erastosthenes highest prime factor in erlang

I have edited the program so that it works(with small numbers) however I do not understand how to implement an accumulator as suggested. The reason why is because P changes throughout the process, therefore I do not know in with which granularity I…
0
votes
3 answers

optimizing the code for sieve

Here is the code that i have written for finding prime numbers between a range where the upper bound can be as big as 1000000000.I have used Hashmap and have not stored any even number except 2 and havent stored sero and one as well.But still when I…
djscribbles
  • 73
  • 3
  • 7
0
votes
3 answers

Find the prime-->sieve way

I tried it several times but still gives me ArrayOutOfIndex. But i want to save the memory so i use boolean[]isPrime = new boolean [N/2+1]; instead of boolean[]isPrime = new boolean [N+1]; This gives me ArrayOutOfIndex for line 23 and 47 line…
jackhao
  • 3,457
  • 3
  • 22
  • 43
0
votes
3 answers

Sieve of Eratosthenes wrong output

I was trying to find out the sum of all prime up to 2 million. So I wrote the following code for it: #include #include #define limit 2000000 int main(void) { unsigned int *sieve, i, j; unsigned long long int sum = 0; …
House
  • 95
  • 2
  • 12
0
votes
4 answers

Print first 1 million primes in 1 sec with constraints program size 50000 bytes and limited Memory

I tried sieve of Eratosthenes: Following is my code: void prime_eratos(int N) { int root = (int)sqrt((double)N); bool *A = new bool[N + 1]; memset(A, 0, sizeof(bool) * (N + 1)); for (int m = 2; m <= root; m++) { if (!A[m]) { …
vijay
  • 2,034
  • 3
  • 19
  • 38