Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/128/12802/c.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   500000
   28 #define MAX_PRIMES 79500
   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=1,j=3; i<MAX_SIZE; i++,j=j+2)
   48         {
   49             buff[i]=j;
   50         }
   51     for (i=1; i<MAX_SIZE; i++)
   52         {
   53             /* loop through primes */
   54             if (0 != buff[i])
   55                 {
   56                     /* found a prime */
   57                     primes[numPrimes] = buff[i];
   58                     numPrimes++;
   59                     for (j=i+buff[i]; j<MAX_SIZE; j=j+buff[i])
   60                         {
   61                             /* mark out multiples */
   62                             buff[j] = 0;
   63                         } /* mark out multiples */
   64                 } /* found a prime */
   65         } /* loop through primes */
   66     printf("numPrimes = %d\n", numPrimes);
   67 } /* FUNCTION init */
   68 
   69 int isPrime(int num)
   70 {
   71     /* FUNCTION isPrime */
   72     int ip;
   73     int i;
   74 
   75     if (1 == num)
   76         {
   77             /* 1 is considered prine */
   78             ip = TRUE;
   79         } /* 1 is considered prine */
   80     else
   81         {
   82             /* not a 1 -- figure out if it is prime */
   83             ip = FALSE;
   84             for (i=0; ((! ip) && (i<numPrimes)); i++)
   85                 {
   86                     /* search prime list */
   87                     ip = (num == primes[i]);
   88                 } /* search prime list */
   89             if (! ip)
   90                 {
   91                     /* not one fo the first primes -- test it for being prime */
   92                     for (i=0; (!ip) && (i<numPrimes) && ((primes[i] * primes[i]) < num); i++)
   93                         {
   94                             /* check divisibility */
   95                             ip = (0 != (num % primes[i]));
   96                         } /* check divisibility */
   97                 } /* not one fo the first primes -- test it for being prime */
   98         } /* not a 1 -- figure out if it is prime */
   99     return ip;
  100 } /* FUNCTION isPrime */
  101 
  102 
  103 int isPalin(int num)
  104 {
  105     /* FUNCTION isPalin */
  106     int tmp;
  107     int rev;
  108 
  109     tmp = num;
  110     rev = 0;
  111     while (1 < tmp)
  112         {
  113             /* keep going */
  114             rev = (rev * 10) + (tmp % 10);
  115             tmp = tmp / 10;
  116         } /* keep going */
  117     DEBUG1 printf("isPalin: num %d   rev %d\n", num, rev);
  118     return ((1 == num) || (num == rev));
  119 } /* FUNCTION isPalin */
  120 
  121 void dump()
  122 {
  123     /* FUNCTION dump */
  124 } /* FUNCTION dump */
  125 
  126 void getInput()
  127 {
  128     /* FUNCTION getInput */
  129 
  130     scanf(" %d ", &num);
  131     DEBUG1 printf("getInput: num = %d\n", num);
  132 } /* FUNCTION getInput */
  133 
  134 void process()
  135 {
  136     /* FUNCTION process */
  137     printf("%d\n", num*2);
  138 } /* FUNCTION process */
  139 
  140 int main()
  141 {
  142     /* main */
  143     int timeToStop = FALSE;
  144     int tmp1;
  145     int tmp2;
  146 
  147     init();
  148     while (! timeToStop)
  149         {
  150             /* while */
  151             getInput();
  152             process();
  153             timeToStop = (isPrime(num) && isPalin(num));
  154         } /* while */
  155 
  156     return EXIT_SUCCESS;
  157 } /* main */
  158