The Sieve of Eratosthenes

Regarding this topic, we'd already done the program code..but we are not sure whether our solution/code satisfy the theory of the sieve of eratosthenes..?could anyone pls verify it for us..that we'd done the right steps???tq..


[#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define SIZE 1000

void color (int color);
int main()
{

int prime_num[ SIZE ], counter, division; // Variable declarations.

color(14);
printf("\t \n");

color(14);
printf("##########################################################################\n\n");

color(10);
printf("\n.............WELCOME TO THE SIEVE OF ERATOSTHENES PROGRAM.................\n\n\n");

color(14);
printf("##########################################################################\n\n");

color(7);
printf("This program is designed to find the prime numbers between 1 to 1000.\n\n");

printf("The prime numbers are:\n\n");

for( counter = 0; counter < SIZE; counter++ ) // sets all the elements to 1
prime_num[ counter ] = 1;


for( counter = 2; counter < SIZE; counter++ ) // set prime subscripted to 0

if( prime_num[ counter ] ) // if prime_number[ counter ] == 1


for( division = counter + 1; division < SIZE; division++ )// checks if it is divisible other than 1 or itself.

if( !(division % counter) ) // if not then set as 0.
prime_num[ division ] = 0;


for( counter = 2; counter < SIZE; counter++ )// print all the prime subscripted, that are initialized to 1.
if( prime_num[ counter ] )
{
color(11);
printf("%d\t", counter);
}
color(9);

printf("\n\n\n");
system("pause");
return 0;
}

void color (int color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}

]
I would say no, with the sieve you don't need to check if the number is prime. Just iterate over the array and for each index that is set prime set all multiples not prime.
Topic archived. No new replies allowed.