Computer Programming Contest Preparation

ToolBox - Source for: 102/10235/a.c



/home/toolbox/public_html/solutions/102/10235/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 #define MAX_LINE 257
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2016-03-29
   20  * Purpose: fun
   21  * Problem: 10235 - Simply Emirp
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 #define MAXX 1000001
   29 
   30 int num;
   31 int primes[MAXX];
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     int i;
   37     int j;
   38 
   39     for (i=2; MAXX>i; i++)
   40         {
   41             primes[i] = 1;
   42         }
   43     for (i=2; MAXX>i; i++)
   44         {
   45             /* for */
   46             if (1 == primes[i])
   47                 {
   48                     /* found a prime */
   49                     for (j=i+i; MAXX>j; j=j+i)
   50                         {
   51                             primes[j] = 0;
   52                         }
   53                 } /* found a prime */
   54         } /* for */
   55 } /* FUNCTION init */
   56 
   57 void dump()
   58 {
   59     /* FUNCTION dump */
   60 } /* FUNCTION dump */
   61 
   62 int revPrime()
   63 {
   64     /* FUNCTION revPrime */
   65     int i;
   66     int tst = 0;
   67 
   68     i = num;
   69     while (0 < i)
   70         {
   71             /* while */
   72             tst = (tst * 10) + i % 10;
   73             i = i / 10;
   74         } /* while */
   75     DEBUG printf("%d reversed is %d\n", num, tst);
   76     return ((num != tst) && (1 == primes[tst]));
   77 } /* FUNCTION revPrime */
   78 
   79 int getInput()
   80 {
   81     /* FUNCTION getInput */
   82     int dataReadFlag;
   83 
   84     dataReadFlag = (1 == scanf(" %d ", &num));
   85     return (dataReadFlag);
   86 } /* FUNCTION getInput */
   87 
   88 void process()
   89 {
   90     /* FUNCTION process */
   91     if (0 == primes[num])
   92         {
   93             /* not prime */
   94             printf("%d is not prime.\n", num);
   95         } /* not prime */
   96     else
   97         {
   98             /* only primes left */
   99             if (revPrime())
  100                 {
  101                     /* emirp */
  102                     printf("%d is emirp.\n", num);
  103                 } /* emirp */
  104             else
  105                 {
  106                     /* merely prime */
  107                     printf("%d is prime.\n", num);
  108                 } /* merely prime */
  109         } /* only primes left */
  110 } /* FUNCTION process */
  111 
  112 int main()
  113 {
  114     /* main */
  115     int moreToDo;
  116 
  117     init();
  118     moreToDo = getInput();
  119     while (moreToDo)
  120         {
  121             /* while */
  122             process();
  123             moreToDo = getInput();
  124         } /* while */
  125 
  126     return EXIT_SUCCESS;
  127 } /* main */
  128