Computer Programming Contest Preparation

ToolBox - Source for: 16/1644/aa.c



/home/toolbox/public_html/solutions/16/1644/aa.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: 1644
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_PRIMES 100005
   28 #define MAX_SIZE 1299750
   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     if (0 != buff[num])
   94         {
   95             /* number read in was prime -- output 0 */
   96             printf("0\n");
   97         } /* number read in was prime -- output 0 */
   98     else
   99         {
  100             /* non-prime read in -- look for bounds */
  101             for (i=num; 0 == buff[i]; i--);
  102             cnt = num - i;
  103             for (i=num; 0 == buff[i]; i++);
  104             cnt = cnt + (i - num);
  105             printf("%d\n", cnt);
  106         } /* non-prime read in -- look for bounds */
  107 
  108 
  109 } /* FUNCTION process */
  110 
  111 int main ()
  112 {
  113     /* main */
  114     int moreToDo;
  115 
  116     init();
  117     moreToDo = getInput();
  118     while (moreToDo)
  119         {
  120             /* while */
  121             process();
  122             moreToDo = getInput();
  123         } /* while */
  124 
  125     return EXIT_SUCCESS;
  126 } /* main */
  127