Finding prime numbers with Sieve of Eratosthenes algorithm in c

74 Views Asked by At

I am trying to find the first 1000 prime numbers using an algorithm called "Sieve of Eratosthenes" Basically, you have a list of ascending numbers 2,3,4 ... and this list represents potential prime numbers; Number 2 is prime so it is printed, then, we check the list for any multiples of 2. If we find any, we omit them. And keep going till we have our correct list.

I thought of using nested loops for this one, and although I don't feel like it's a perfect idea; I don't see why it won't work. Here is my code. It doesn't run at all

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

int main()
{
    int primelist[999];
    int n = 2;

    for (int i = 1; i <= 999; i++)
    {
        primelist[i] = i;
    }

    for (int i = 0; i <= 999; i++, n++)
    {
        while (primelist[i] != 0) {
            for (int i = 0; i <= 999; i++)
            {
            if ( (primelist[n] % primelist[i]) == 0 )
            {
                primelist[i] = 0;
            }
            }
    }
    }
}

0

There are 0 best solutions below