Computer Programming Contest Preparation

ToolBox - Source for: 6/686/a.c



/home/toolbox/public_html/solutions/6/686/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2015-03-13
   19  * Purpose: practice
   20  * Problem: 10699
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_PRIMES 42000
   28 #define MAX_SIZE 500000
   29 
   30 int num;
   31 int primes[MAX_PRIMES];
   32 int buff[MAX_SIZE];
   33 int numPrimes;
   34 
   35 
   36 int init()
   37 {
   38     /* FUNCTION init */
   39     int i;
   40     int j;
   41 
   42     numPrimes = 1;
   43     primes[1] = 2;
   44     buff[0] = 0;
   45     buff[1] = 0;
   46     buff[2] = 2;
   47 
   48     /* sieve of erasthones */
   49     for (i=3; i<MAX_SIZE; i=i+2)
   50         {
   51             buff[i]=i;
   52             buff[i+1]=0;
   53         }
   54     for (i=3; i<MAX_SIZE; i=i+2)
   55         {
   56             /* loop through primes */
   57             if (0 != buff[i])
   58                 {
   59                     /* found a prime */
   60                     numPrimes++;
   61                     primes[numPrimes] = i;
   62                     for (j=i+i+i; j<MAX_SIZE; j=j+i+i)
   63                         {
   64                             /* mark out multiples */
   65                             buff[j] = 0;
   66                         } /* mark out multiples */
   67                 } /* found a prime */
   68         } /* loop through primes */
   69 } /* FUNCTION init */
   70 
   71 int dump()
   72 {
   73     /* FUNCTION dump */
   74 } /* FUNCTION dump */
   75 
   76 int getInput()
   77 {
   78     /* FUNCTION getInput */
   79     int dataReadFlag;
   80 
   81     scanf(" %d ", &num);
   82     dataReadFlag = (0 != num);
   83     return dataReadFlag;
   84 } /* FUNCTION getInput */
   85 
   86 void process()
   87 {
   88     /* FUNCTION process */
   89     int i;
   90     int tmp;
   91     int cnt = 0;
   92 
   93     tmp = 4;
   94     for (i=1; tmp<=num; i++)
   95         {
   96             /* for */
   97             tmp = num - primes[i];
   98             DEBUG   printf("num: %d  prime[%d]: %d  tmp: %d  flag: %d\n", num, i, primes[i], tmp, buff[tmp]);
   99             if (buff[tmp] != 0)   /* found a matching prime */
  100                 {
  101                     /* found */
  102                     cnt++;
  103                 } /* found */
  104             tmp = primes[i+1] + primes[i+1];
  105         } /* for */
  106     printf("%d\n", cnt);
  107 } /* FUNCTION process */
  108 
  109 int main ()
  110 {
  111     /* main */
  112     int moreToDo;
  113 
  114     init();
  115     moreToDo = getInput();
  116     while (moreToDo)
  117         {
  118             /* while */
  119             process();
  120             moreToDo = getInput();
  121         } /* while */
  122 
  123     return EXIT_SUCCESS;
  124 } /* main */
  125