# the sieve of eratosthenes

Page 1 of 1

## 6 Replies - 766 Views - Last Post: 25 July 2013 - 09:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=325604&amp;s=786db2eb4957a4157a56b3c4a4ca80d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hyeza

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-July 13

# the sieve of eratosthenes

Posted 25 July 2013 - 08:47 AM

hi,could you pls check our coding whether it satisfies the steps to calculate prime nos. using the theory of the sieve of eratosthenes...?
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");

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);
}
```

This post has been edited by modi123_1: 25 July 2013 - 08:48 AM
Reason for edit:: please use code tags

Is This A Good Question/Topic? 0

## Replies To: the sieve of eratosthenes

### #2 modi123_1

• Suitor #2

Reputation: 14501
• Posts: 58,134
• Joined: 12-June 08

## Re: the sieve of eratosthenes

Posted 25 July 2013 - 08:52 AM

Well.. what happens when you run that slightly modified version of code from 2003? [.] Do you get your results?

### #3 hyeza

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-July 13

## Re: the sieve of eratosthenes

Posted 25 July 2013 - 08:59 AM

modi123_1, on 25 July 2013 - 08:52 AM, said:

Well.. what happens when you run that slightly modified version of code from 2003? . Do you get your results?

we are using software dev c++..n the attached file is the result that we obtain..now, we just want to make sure that the coding satisfies the sieve of eratosthenes's theory?

### #4 modi123_1

• Suitor #2

Reputation: 14501
• Posts: 58,134
• Joined: 12-June 08

## Re: the sieve of eratosthenes

Posted 25 July 2013 - 09:02 AM

I would believe you can hand check those results, right? I mean that shouldn't be the hard part to cram them in the theorem, right?

https://en.wikipedia...of_Eratosthenes

### #5 hyeza

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-July 13

## Re: the sieve of eratosthenes

Posted 25 July 2013 - 09:25 AM

we had been given a task regarding this topic.
the attached files are the question and the output of our coding;below is our coding.
now, we just wanna make sure that whether we had followed the given instructions.

```#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");

for( counter = 0; counter < SIZE; counter++ ) // sets all the elements to 1
{   prime_num[ counter ] = 1;
printf("%d\t", counter);
}
for( counter = 2; counter < SIZE; counter++ ) // set prime subscripted to 0
{
if( prime_num[ counter ] )                // if prime_number[ counter ] == 1
printf("%d\t", counter);
}
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;
printf("%d\t", counter);
}
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);
}
```

#### Attached image(s)

This post has been edited by jimblumberg: 25 July 2013 - 09:28 AM
Reason for edit:: Added missing code tags, please learn to use them when posting code.

### #6 BetaWar

• #include "soul.h"

Reputation: 1562
• Posts: 8,352
• Joined: 07-September 06

## Re: the sieve of eratosthenes

Posted 25 July 2013 - 09:28 AM

### #7 modi123_1

• Suitor #2

Reputation: 14501
• Posts: 58,134
• Joined: 12-June 08

## Re: the sieve of eratosthenes

Posted 25 July 2013 - 09:31 AM

AGain I do not understand why you are not able to check this by hand. You have a page of results.. go through the sieve's steps and spot check your answer.

http://www.murderous...s/MMoE/erat.htm