Computer Programming Contest Preparation

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



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