Computer Programming Contest Preparation

ToolBox - Source for: 128/12802/b.c



/home/toolbox/public_html/solutions/128/12802/b.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 #define DEBUG1 if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date:
   19  * Purpose: fun
   20  * Problem:
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_SIZE   1000
   28 #define MAX_PRIMES 175
   29 
   30 int num;
   31 int primes[MAX_PRIMES];
   32 int numPrimes;
   33 int buff[MAX_SIZE];
   34 
   35 
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40     int i;
   41     int j;
   42 
   43     numPrimes = 1;
   44     primes[0] = 2;
   45 
   46     /* sieve of erasthones */
   47     for (i=3; i<MAX_SIZE; i=i+2) buff[i]=i;
   48     for (i=3; i<MAX_SIZE; i=i+2)
   49         {
   50             /* loop through primes */
   51             if (0 != buff[i])
   52                 {
   53                     /* found a prime */
   54                     primes[numPrimes] = i;
   55                     numPrimes++;
   56                     for (j=i; j<MAX_SIZE; j=j+i+i)
   57                         {
   58                             /* mark out multiples */
   59                             buff[j] = 0;
   60                         } /* mark out multiples */
   61                 } /* found a prime */
   62         } /* loop through primes */
   63 } /* FUNCTION init */
   64 
   65 int isPrime(int num)
   66 {
   67     /* FUNCTION isPrime */
   68     int i;
   69     int prim;
   70 
   71     prim = (1 == num); /* consider 1 prime */
   72     DEBUG1 printf("isPrime: num = %d   ip = %d\n", num, prim);
   73     for (i=0; ((prim) && (i<numPrimes)); i++)
   74         {
   75             /* check for divisor */
   76             prim = (0 != (num % primes[i]));
   77             if ((primes[i] * primes[i]) < num)
   78                 {
   79                     i = numPrimes + 10;
   80                 }
   81             DEBUG1 printf("isPrime: num = %d   prim = %d", num, primes[i]);
   82             DEBUG1 printf("  ip = %d\n", prim);
   83         } /* check for divisor */
   84     DEBUG1 printf("isPrime: isPrime %d\n", prim);
   85     return (prim);
   86 } /* FUNCTION isPrime */
   87 
   88 int isPalin(int num)
   89 {
   90     /* FUNCTION isPalin */
   91     int tmp;
   92     int rev;
   93 
   94     tmp = num;
   95     rev = 0;
   96     while (1 < tmp)
   97         {
   98             /* keep going */
   99             rev = (rev * 10) + (tmp % 10);
  100             tmp = tmp / 10;
  101         } /* keep going */
  102     DEBUG1 printf("isPalin: num %d   rev %d\n", num, rev);
  103     return ((1 == num) || (num == rev));
  104 } /* FUNCTION isPalin */
  105 
  106 void dump()
  107 {
  108     /* FUNCTION dump */
  109 } /* FUNCTION dump */
  110 
  111 void getInput()
  112 {
  113     /* FUNCTION getInput */
  114 
  115     scanf(" %d ", &num);
  116     DEBUG1 printf("getInput: num = %d\n", num);
  117 } /* FUNCTION getInput */
  118 
  119 void process()
  120 {
  121     /* FUNCTION process */
  122     printf("%d\n", num*2);
  123 } /* FUNCTION process */
  124 
  125 int main()
  126 {
  127     /* main */
  128     int moreToDo = TRUE;
  129     int tmp1;
  130     int tmp2;
  131 
  132     init();
  133     getInput();
  134     while (moreToDo)
  135         {
  136             /* while */
  137             process();
  138             moreToDo = (! (isPrime(num) && isPalin(num)));
  139             tmp1 = isPrime(num);
  140             tmp2 = isPalin(num);
  141             DEBUG1 printf("main: moreToDo %d   isPrime %d    isPalin %d   num %d\n", moreToDo, tmp1, tmp2, num);
  142             getInput();
  143         } /* while */
  144 
  145     return EXIT_SUCCESS;
  146 } /* main */
  147