Sieve of Eratosthenes

sieve of eratosthenes worksheet
sieve of eratosthenes time complexity
sieve of eratosthenes python
sieve of eratosthenes java
sieve of eratosthenes calculator
sieve of eratosthenes interactive
sieve of eratosthenes c#
sieve of eratosthenes pronunciation

I read up on the sieve of Eratosthenes while solving a question on Project Euler. I'm sure you guys know which question im talking about. So here's the thing. My code manages to show all the primes under 1 million correctly. However when i try the same implementation for 2 million it's giving me a segmentation fault... I have a certain idea of why the error is coming but don't know how to correct it... Here's the code for primes under 1 million.

#include<stdio.h>
int main(void)
{
   int i,k=2;
   int j;
   int n=1000000;
   int prime[2000000]={};
   for(i=0;i<n;i++) // initializes the prime number array
   {
      prime[i]=i;
   }
   for(i=2;i<n;i++) // Implementation of the Sieve
   {
      if(prime[i]!=0)
      { 
         for(j=2;j<n;j++)
         {
            {
               prime[j*prime[i]]=0;
               if(prime[i]*j>n)
                  break;    
            }
         }
      }
   }
   for(i=0;i<n;i++) // Prints the prime numbers
      if(prime[i]!=0)
      {
         printf("%d\n"prime[i]);
      }
      return(0);
   }
}

You're allocating a huge array in stack:

int prime[2000000]={};

Four bytes times two million equals eight megabytes, which is often the maximum stack size. Allocating more than that results in segmentation fault.

You should allocate the array in heap, instead:

int *prime;
prime = malloc(2000000 * sizeof(int));
if(!prime) {
    /* not enough memory */
}
/* ... use prime ... */
free(prime);

Sieve of Eratosthenes, by iteratively marking the multiples of primes as composite, starting from 2. The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so (Ref Wiki). Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution.

Here is my implementation.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int* sieve(int n) {
  int* A = calloc(n, sizeof(int));
  for(int i = 2; i < (int) sqrt(n); i++) {
    if (!A[i]) {
      for (int j = i*i; j < n; j+=i) {
        A[j] = 1;
      }
    }
  }
  return A;
}

I benchmarked it for the first 1,000,000,000 numbers on an i5 Kaby Lake.

🐻 time ./sieve 1000000000
./sieve 1000000000  16.21s user 1.05s system 99% cpu 17.434 total

I simply translated this pseudocode from Wikipedia.

Sieve of Eratosthenes, , and is a simple yet time efficient algorithm for finding all the primes below a certain limit. Sieve of Eratosthenes An algorithm for making tables of primes. Sequentially write down the integers from 2 to the highest number you wish to include in the table. Cross out all numbers which are divisible by 2 (every second number).

Here was my implementation (Java) much simpler in that you really only need one array, just start for loops at 2.

edit: @cheesehead 's solution was probably better, i just read the description of the sieve and thought it would be a good thought exercise.

      // set max;
      int max = 100000000;

      // logic
      boolean[] marked = new boolean[max]; // all start as false
      for (int k = 2; k < max;) {
         for (int g = k * 2; g < max; g += k) {
            marked[g] = true;
         }
         k++;
         while (k < max && marked[k]) {
            k++;
         }
      }

      //print
      for (int k = 2; k < max; k++) {
         if (!marked[k]) {
            System.out.println(k);
         }
      }

Sieve of Eratosthenes, Sieve of Eratosthenes allows us to generate a list of primes. Created by Brit Cruise. Google Duration: 4:12 Posted: Sep 4, 2014 Sieve of Eratosthenes is a simple and ancient algorithm used to find the prime numbers up to any given limit. It is one of the most efficient ways to find small prime numbers. For a given upper limit n n the algorithm works by iteratively marking the multiples of primes as composite, starting from 2.

Introduction - Python Prime Number Sieves, Sieve of Eratosthenes is a simple and ancient algorithm used to find the prime numbers up to any given limit. It is one of the most efficient ways to find small prime numbers. For a given upper limit n the algorithm works by iteratively marking the multiples of primes as composite, starting from 2. The Sieve of Eratosthenes. Not all numbers are hard to identify as prime or composite (not prime). For example, any even number larger than 2 is composite. However, while some numbers can be easy to classify, others can be extremely difficult. Fortunately, there are some tricks that can help, at least under some circumstances.

Sieve of Eratosthenes (video), Sieve of Eratosthenes is an algorithm for finding all the prime numbers in a segment [1;n] using O(nloglogn) operations. The algorithm is very simple: at the  Sieve of Eratosthenes: algorithm steps for primes below 121 (including optimization of starting from prime's square). In mathematics , the sieve of Eratosthenes ( Greek : κόσκινον Ἐρατοσθένους {{#invoke:Category handler|main}}), one of a number of prime number sieves , is a simple, ancient algorithm for finding all prime

Sieve of Eratosthenes, Sieve of Eratosthenes. The most efficient way to find all of the small primes (say all those less than 10,000,000) is by using a sieve such as the  Voiceover: I'm now going to introduce an ancient method for generating a list of primes up to some limit N, called the Sieve of Erathosthenes. Now Erathosthenes was born in 276 BC. So this method was over 2200 years old. But it's very simple and elegant and you can teach it to any child.

Comments
  • Did you for get to change int n=1000000; to int n=2000000;
  • This does look like a possibly out of bounds array access: prime[j*prime[i]]=0.
  • Of side note, you probably should be using some other data type than int. Int is not guaranteed to be any particular size, other than 16 bit. As a style issue I would recommend long for numbers above 32k.
  • If he's going to be indexing a big array, he might as well use size_t