The Sieve of Eratosthenes and Goldbach's Conjecture

Assignment: http://web.njit.edu/~kapleau/teach/current/cs115/project.php#prj2

Implement the Sieve of Eratosthenes and use it to find all prime
numbers less than or equal to one million. Use the result to
prove Goldbach's Conjecture for all even integers between four and
one million, inclusive.

Implement a function with the following declaration:

void sieve(int array[], int num);
This function takes an integer array as its argument. The array
should be initialized to the values 1 through 1000000. The
function modifies the array so that only the prime numbers remain;
all other values are zeroed out.
This function must be written to accept an integer array of any
size. You must should output for all primes numbers between 1 and
1000000, but when I test your function it may be on an array of a
different size.

Implement a function with the following declaration:

void goldbach(int array[], int num);
This function takes the same argument as the previous function
and displays each even integer between 4 and 1000000 with two
prime numbers that add to it.
The goal here is to provide an efficient implementation. This
means no multiplication, division, or modulus when determining if
a number is prime. It also means that the second function must find
two primes efficiently.

Output for your program: All prime numbers between 1 and 1000000
and all even numbers between 4 and 1000000 and the two prime
numbers that sum up to it.

This is code so far. HELP PLEASE!!!

1
2
3
4
5
6
7
8
9
10
void sieve( int array[], int num ) { 
    if ( num < 2 ) return; /* do nothing if nothing to do */ 
    array[0]= array[1]= 0; /* zero and one are NOT prime */ 
    for ( int i= 2; i < num; ++i ) 
        array[i]= i; 
    for ( int i= 0; i < num; ++i ) 
        if ( array[i] != 0 ) 
            for ( int j= i+i; j < num; j += i ) 
                array[j]= 0; 
} 
line 6 of your function, will create infinite loop on line 8. You can safely assume that 0 and 1 are not prime, then start your loop at int i = 2
Topic archived. No new replies allowed.