Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/100/10042/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 
   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     if (tmp > 9)
   81         {
   82             /* still more than 1 digit */
   83             tmp = sumDigits((tmp % 10 ) + sumDigits(tmp / 10));
   84         } /* still more than 1 digit */
   85     return tmp;
   86 } /* FUNCTION sumDigits */
   87 
   88 int factor(int tmp)
   89 {
   90     /* FUNCTION factor */
   91     int toReturn = 0;
   92     int i;
   93     int divisorCnt = 0;
   94 
   95     if (0 < tmp)
   96         {
   97             /* something to do */
   98             i = 0;
   99             while ((i<=numPrimes) && (tmp > primes[i]))
  100                 {
  101                     /* possible factor */
  102                     if (0 == (tmp % primes[i]))
  103                         {
  104                             /* factor found */
  105                             toReturn = toReturn + primes[i];
  106                             tmp = tmp / primes[i];
  107                             divisorCnt++;
  108                         } /* factor found */
  109                     else
  110                         {
  111                             /* not a divisor -- go to next prime */
  112                             i++;
  113                         } /* not a divisor -- go to next prime */
  114                 } /* possible factor */
  115             if (0 < tmp)
  116                 {
  117                     /* remaining tmp mut be prime */
  118                     toReturn = toReturn + tmp;
  119                     divisorCnt++;
  120                 } /* remaining tmp mut be prime */
  121             if (2 > divisorCnt)
  122                 {
  123                     /* got us a prime number */
  124                     toReturn = -1;
  125                 } /* got us a prime number */
  126             else
  127                 {
  128                     /* has at least 1 factor already */
  129                     toReturn = sumDigits(toReturn);
  130                 } /* has at least 1 factor already */
  131         } /* something to do */
  132     return toReturn;
  133 } /* FUNCTION factor */
  134 
  135 void process()
  136 {
  137     /* FUNCTION process */
  138     int i;
  139     int sum;
  140     int facSum;
  141 
  142     num++;
  143     sum = sumDigits(num);
  144     facSum = factor(num);
  145     while (facSum != sum)
  146         {
  147             /* not found yet */
  148             num++;
  149             sum = sumDigits(num);
  150             facSum = factor(num);
  151         } /* not found yet */
  152     printf("%d\n", num);
  153 } /* FUNCTION process */
  154 
  155 int main ()
  156 {
  157     /* main */
  158     int i;
  159     int cnt;
  160 
  161     init();
  162     scanf(" %d ", &cnt);
  163     for (i=0; i<cnt; i++)
  164         {
  165             /* for */
  166             getInput();
  167             process();
  168         } /* for */
  169     return EXIT_SUCCESS;
  170 } /* main */
  171