The basic algorithm is
- Write down all of the numbers between 2 and n, the largest possible prime you are interested in
- Set the current prime to be 2.
- While the current prime is less than the square root of the largest possible prime
- Cross off all of the multiples of the current prime.
- Find the next number that has not been crossed off, this is the new current prime.
- List all of the numbers, 2 and higher, that have not been crossed off. These are the prime numbers.
We will write this program in a series of guided steps. Please follow these steps. Your code is expected to be the same structure as mine.
sieve
void InitSieve(bool sieve[]);
void PrintSieve(const bool sieve[]);
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTT
InitSieve(sieve); if (DEBUG) { PrintSieve(sieve); cout << endl; }
if (DEBUG)
statement in this project.
current
to main. This is an integer and should be initialized to 2, as the first prime number.
void DoSieve(bool sieve[], int prime);
DoSieve(sieve,current); if (DEBUG) { PrintSieve(sieve); cout << endl; }
TTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF TFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF TFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF
int FindNextPrime(const bool sieve[], int current);
while (current < sqrt(MAX_PRIME)) { if (DEBUG) { cout << "The next prime is " << current << endl; } DoSieve(sieve,current); if (DEBUG) { PrintSieve(sieve); cout << endl; } current = FindNextPrime(sieve, current); }
[bennett@mirkwood me]$ sieve TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT ... The next prime is 2 TTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF TFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF ... The next prime is 3 TTFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFF TFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTFFFTFTF ... The next prime is 5 TTFTFTFFFTFTFFFTFTFFFTFFFFFTFTFFFFFTFFFTFTFFFTFTFFFTFFFFFTFTFFFFFTFFF TFTFFFTFTFFFTFFFFFTFTFFFFFTFFFTFTFFFTFTFFFTFFFFFTFTFFFFFTFFFTFTFFFTFTF ... The next prime is 7 TTFTFTFFFTFTFFFTFTFFFTFFFFFTFTFFFFFTFFFTFTFFFTFFFFFTFFFFFTFTFFFFFTFFF TFTFFFFFTFFFTFFFFFTFFFFFFFTFFFTFTFFFTFTFFFTFFFFFFFTFFFFFTFFFTFFFFFTFTF ... The next prime is 11 TTFTFTFFFTFTFFFTFTFFFTFFFFFTFTFFFFFTFFFTFTFFFTFFFFFTFFFFFTFTFFFFFTFFF TFTFFFFFTFFFTFFFFFTFFFFFFFTFFFTFTFFFTFTFFFTFFFFFFFFFFFFFTFFFTFFFFFTFTF ... . . . The next prime is 31 TTFTFTFFFTFTFFFTFTFFFTFFFFFTFTFFFFFTFFFTFTFFFTFFFFFTFFFFFTFTFFFFFTFFF TFTFFFFFTFFFTFFFFFTFFFFFFFTFFFTFTFFFTFTFFFTFFFFFFFFFFFFFTFFFTFFFFFTFTF ...
primes
, an array of PRIMES_SIZE
integers.
primeCount
, an integer initialized to 0.
void FindPrimes(const bool sieve[], int primes[], int & primeCount);
primes
array and increment primeCount
void PrintPrimes(const int primes[], int primeCount);
FindPrimes(sieve, primes, primeCount); PrintPrimes(primes, primeCount);
./sieve | diff - list