Computer Programming Contest Preparation

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



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