Computer Programming Contest Preparation

ToolBox - Source for: 100/10042/a.c



/home/toolbox/public_html/solutions/100/10042/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: 2019-12-03
   19  * Purpose: practice
   20  * Problem: 10042
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_SIZE 17500
   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 = 0;
   41     primes[0] = 2;
   42     j = 3;
   43 
   44     /* sieve of erasthones */
   45     for (i=1; i<MAX_SIZE; i=i+1,j=j+2)
   46         {
   47             primes[i]=j;
   48         }
   49     for (i=1; i<MAX_SIZE; i=i+1)
   50         {
   51             /* loop through primes */
   52             if (0 != primes[i])
   53                 {
   54                     /* found a prime */
   55                     numPrimes++;
   56                     primes[numPrimes] = primes[i];
   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 } /* FUNCTION init */
   65 
   66 int dump()
   67 {
   68     /* FUNCTION dump */
   69 } /* FUNCTION dump */
   70 
   71 void getInput()
   72 {
   73     /* FUNCTION getInput */
   74     scanf(" %d ", &num);
   75 } /* FUNCTION getInput */
   76 
   77 int sumDigits(int tmp)
   78 {
   79     /* FUNCTION sumDigits */
   80     int sum = 0;
   81 
   82     while (tmp > 0)
   83         {
   84             /* still more than 1 digit */
   85             sum = sum + (tmp % 10);
   86             tmp = tmp / 10;
   87         } /* still more than 1 digit */
   88     return sum;
   89 } /* FUNCTION sumDigits */
   90 
   91 int factor(int tmp)
   92 {
   93     /* FUNCTION factor */
   94     int toReturn = 0;
   95     int i;
   96     int divisorCnt = 0;
   97 
   98     if (0 < tmp)
   99         {
  100             /* something to do */
  101             i = 0;
  102             while ((i<=numPrimes) && (tmp > primes[i]))
  103                 {
  104                     /* possible factor */
  105                     if (0 == (tmp % primes[i]))
  106                         {
  107                             /* factor found */
  108                             toReturn = toReturn + sumDigits(primes[i]);
  109                             tmp = tmp / primes[i];
  110                             divisorCnt++;
  111                         } /* factor found */
  112                     else
  113                         {
  114                             /* not a divisor -- go to next prime */
  115                             i++;
  116                         } /* not a divisor -- go to next prime */
  117                 } /* possible factor */
  118             if (0 < tmp)
  119                 {
  120                     /* remaining tmp mut be prime */
  121                     toReturn = toReturn + sumDigits(tmp);
  122                     divisorCnt++;
  123                 } /* remaining tmp mut be prime */
  124             if (2 > divisorCnt)
  125                 {
  126                     /* got us a prime number */
  127                     toReturn = -1;
  128                 } /* got us a prime number */
  129         } /* something to do */
  130     return toReturn;
  131 } /* FUNCTION factor */
  132 
  133 void process()
  134 {
  135     /* FUNCTION process */
  136     int i;
  137     int sum;
  138     int facSum;
  139 
  140     num++;
  141     sum = sumDigits(num);
  142     facSum = factor(num);
  143     while (facSum != sum)
  144         {
  145             /* not found yet */
  146             num++;
  147             sum = sumDigits(num);
  148             facSum = factor(num);
  149         } /* not found yet */
  150     printf("%d\n", num);
  151 } /* FUNCTION process */
  152 
  153 int main ()
  154 {
  155     /* main */
  156     int i;
  157     int cnt;
  158 
  159     init();
  160     scanf(" %d ", &cnt);
  161     for (i=0; i<cnt; i++)
  162         {
  163             /* for */
  164             getInput();
  165             process();
  166         } /* for */
  167     return EXIT_SUCCESS;
  168 } /* main */
  169