Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/16/1644/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: 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     buff[0] = 0;
   43     buff[1] = 0;
   44     buff[2] = 2;
   45 
   46     /* sieve of erasthones */
   47     for (i=3; i<MAX_SIZE; i=i+2)
   48         {
   49             buff[i]=i;
   50             buff[i+1]=0;
   51         }
   52     for (i=3; i<MAX_SIZE; i=i+2)
   53         {
   54             /* loop through primes */
   55             if (0 != buff[i])
   56                 {
   57                     /* found a prime */
   58                     for (j=i+i+i; j<MAX_SIZE; j=j+i+i)
   59                         {
   60                             /* mark out multiples */
   61                             buff[j] = 0;
   62                         } /* mark out multiples */
   63                 } /* found a prime */
   64         } /* loop through primes */
   65 } /* FUNCTION init */
   66 
   67 int dump()
   68 {
   69     /* FUNCTION dump */
   70 } /* FUNCTION dump */
   71 
   72 int getInput()
   73 {
   74     /* FUNCTION getInput */
   75     int dataReadFlag;
   76 
   77     scanf(" %d ", &num);
   78     dataReadFlag = (0 != num);
   79     return dataReadFlag;
   80 } /* FUNCTION getInput */
   81 
   82 void process()
   83 {
   84     /* FUNCTION process */
   85     int i;
   86     int tmp;
   87     int cnt = 0;
   88 
   89     if (0 != buff[num])
   90         {
   91             /* number read in was prime -- output 0 */
   92             printf("0\n");
   93         } /* number read in was prime -- output 0 */
   94     else
   95         {
   96             /* non-prime read in -- look for bounds */
   97             for (i=num; 0 == buff[i]; i--);
   98             cnt = num - i;
   99             for (i=num; 0 == buff[i]; i++);
  100             cnt = cnt + (i - num);
  101             printf("%d\n", cnt);
  102         } /* non-prime read in -- look for bounds */
  103 
  104 
  105 } /* FUNCTION process */
  106 
  107 int main ()
  108 {
  109     /* main */
  110     int moreToDo;
  111 
  112     init();
  113     moreToDo = getInput();
  114     while (moreToDo)
  115         {
  116             /* while */
  117             process();
  118             moreToDo = getInput();
  119         } /* while */
  120 
  121     return EXIT_SUCCESS;
  122 } /* main */
  123