Computer Programming Contest Preparation

ToolBox - Source for: 5/543/a.c



/home/toolbox/public_html/solutions/5/543/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  *  Author: Isaac Traxler
   17  *    Date: 2015-03-10
   18  * Purpose:
   19  * Problem: 543
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_NUMBER 1000001
   27 int ary[MAX_NUMBER];  /* array of prime flags */
   28 int num;              /* number to process */
   29 
   30 void init()
   31 {
   32     /* FUNCTION init */
   33     int i;
   34     int j;
   35 
   36     ary[0] = 0;
   37     ary[1] = 0;
   38     for (i=0; i<MAX_NUMBER; i=i+2)
   39         {
   40             ary[i] = 0;    /* turn off even numbers */
   41         }
   42     for (i=3; i<MAX_NUMBER; i=i+2)
   43         {
   44             ary[i] = 1;    /* turn on odd numbers */
   45         }
   46     ary[2] = 1;
   47     for (i=3; i<MAX_NUMBER; i=i+2)
   48         {
   49             /* sieve */
   50             if (1 == ary[i])
   51                 {
   52                     /* found a prime number */
   53                     for (j=(2*i); j<MAX_NUMBER; j=j+i)
   54                         {
   55                             ary[j] = 0;    /* mark off each multiple of i */
   56                         }
   57                 } /* found a prime number */
   58         } /* sieve */
   59     /* ary should now be a boolean array of prime/not prime */
   60 } /* FUNCTION init */
   61 
   62 void dump()
   63 {
   64     /* FUNCTION dump */
   65     int i;
   66 
   67     for (i=2; i<2000; i++) if (1 == ary[i]) printf("%d\n", i);
   68 } /* FUNCTION dump */
   69 
   70 int getInput()
   71 {
   72     /* FUNCTION getInput */
   73     int dataReadFlag;
   74 
   75     scanf(" %d ", &num);
   76     dataReadFlag = (0 != num);
   77     return (dataReadFlag);
   78 } /* FUNCTION getInput */
   79 
   80 int isPrime(int tmp)
   81 {
   82     /* FUNCTION isPrime */
   83     return (1 == ary[tmp]);
   84 } /* FUNCTION isPrime */
   85 
   86 void process()
   87 {
   88     /* FUNCTION process */
   89     int i;
   90     int one;
   91     int two;
   92     int notFound = TRUE;
   93 
   94     if (isPrime(num-2))
   95         {
   96             /* 2 is one number */
   97             one = 2;
   98             two = num - one;
   99         } /* 2 is one number */
  100     else
  101         {
  102             /* look for odd prime number */
  103             for (i=3; (! isPrime(i)) || (! isPrime(num-i)); i=i+2)
  104                 {
  105                     /* do nothing */
  106                     DEBUG printf("try %d = %d(%d) + %d(%d)\n", num, i, ary[i], num-i, ary[num-i]);
  107                 } /* do nothing */
  108             DEBUG printf("try %d = %d(%d) + %d(%d)\n", num, i, ary[i], num-i, ary[num-i]);
  109             one = i;
  110             two = num - i;
  111         } /* look for odd prime number */
  112     printf("%d = %d + %d\n", num, one, two);
  113 } /* FUNCTION process */
  114 
  115 int main()
  116 {
  117     /* main */
  118     int moreToDo;
  119 
  120     init();
  121     moreToDo = getInput();
  122     while (moreToDo)
  123         {
  124             /* while */
  125             process();
  126             moreToDo = getInput();
  127         } /* while */
  128 
  129     return EXIT_SUCCESS;
  130 } /* main */
  131