6 Replies - 723 Views - Last Post: 25 July 2013 - 09:31 AM Rate Topic: -----

#1 hyeza   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • Suitor #2
  • member icon



Reputation: 14094
  • View blog
  • Posts: 56,479
  • 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?
Was This Post Helpful? 1
  • +
  • -

#3 hyeza   User is offline

  • New D.I.C Head

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

Re: the sieve of eratosthenes

Posted 25 July 2013 - 08:59 AM

View Postmodi123_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?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14094
  • View blog
  • Posts: 56,479
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 hyeza   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)

  • Attached Image

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.

Was This Post Helpful? 0
  • +
  • -

#6 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1513
  • View blog
  • Posts: 8,290
  • Joined: 07-September 06

Re: the sieve of eratosthenes

Posted 25 July 2013 - 09:28 AM

Merged. Please don't post duplicate threads.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14094
  • View blog
  • Posts: 56,479
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1