Computer Programming Contest Preparation

ToolBox - Source for: 1/160/a.c



/home/toolbox/public_html/solutions/1/160/a.c
    1 #include <stdio.h>
    2 #include <strings.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 
    8 #define TRUE  (1 == 1)
    9 #define FALSE (1 != 1)
   10 
   11 #define DEBUG if (FALSE)
   12 
   13 
   14 /* fprintf(stderr, "functionName: message", varslist); */
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2015-03-11
   19  * Purpose:
   20  * Problem: 160
   21  */
   22 
   23 /*
   24  * This template reads data a specified number of times.
   25  */
   26 
   27 #define MAXPRIMES 26
   28 #define MAX 25
   29 #define UPPERLIMIT 101
   30 
   31 int num;
   32 int primeCnt = 25;
   33 int primes[MAXPRIMES] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
   34 int factors[MAXPRIMES][UPPERLIMIT];
   35 
   36 void factor(int num)
   37 {
   38     /* FUNCTION factor */
   39     int n;
   40     int p = 0;
   41 
   42     n = num;
   43     DEBUG printf("factoring: %d\n", num);
   44     while (1 < n)
   45         {
   46             /* while */
   47             if (0 == (n % primes[p]))
   48                 {
   49                     /* found a factor */
   50                     factors[p][num] = factors[p][num] + 1;
   51                     n = n / primes[p];
   52                     if (p > factors[MAX][num])
   53                         {
   54                             factors[MAX][num] = p;
   55                         }
   56                     DEBUG printf(" primes[%d]=%d  factors[%d][%d]=%d  n=%d\n", p, primes[p], p, num, factors[p][num], n);
   57                 } /* found a factor */
   58             else
   59                 {
   60                     /* was not a factor -- go to next possible factor */
   61                     p++;
   62                 } /* was not a factor -- go to next possible factor */
   63         } /* while */
   64 } /* FUNCTION factor */
   65 
   66 int init()
   67 {
   68     /* FUNCTION init */
   69     int i;
   70     int j;
   71 
   72     for (i=0; i<MAXPRIMES; i++)
   73         {
   74             /* for */
   75             for (j=0; j<UPPERLIMIT; j++)
   76                 {
   77                     /* for */
   78                     factors[i][j] = 0;
   79                 } /* for */
   80         } /* for */
   81 
   82     for (i=2; i<UPPERLIMIT; i++)
   83         {
   84             /* for */
   85             factors[MAX][i] = factors[MAX][i-1];
   86             for (j=0; j<=factors[MAX][i]; j++)
   87                 {
   88                     /* copy over previous numbers factors */
   89                     factors[j][i] = factors[j][i-1];
   90                 } /* copy over previous numbers factors */
   91             factor(i);
   92         } /* for */
   93 
   94 } /* FUNCTION init */
   95 
   96 void dump()
   97 {
   98     /* FUNCTION dump */
   99 } /* FUNCTION dump */
  100 
  101 int getInput()
  102 {
  103     /* FUNCTION getInput */
  104     int dataReadFlag;
  105 
  106     scanf("%d ", &num);
  107     dataReadFlag = (0 != num);
  108 
  109     return dataReadFlag;
  110 } /* FUNCTION getInput */
  111 
  112 void process()
  113 {
  114     /* FUNCTION process */
  115     int i;
  116     int cnt = 0;
  117 
  118     printf("%3d! =", num);
  119     for (i=0; i<=factors[MAX][num]; i++)
  120         {
  121             /* for - print each count */
  122             if (15 == cnt)
  123                 {
  124                     /* max display width reached */
  125                     printf("\n      ");
  126                     cnt = -1;
  127                 } /* max display width reached */
  128             cnt++;
  129             printf("%3d", factors[i][num]);
  130         } /* for - print each count */
  131     printf("\n");
  132 } /* FUNCTION process */
  133 
  134 int main ()
  135 {
  136     /* main */
  137     int moreToDo;
  138 
  139     init();
  140     moreToDo = getInput();
  141     while (moreToDo)
  142         {
  143             /* while */
  144             process();
  145             moreToDo = getInput();
  146         } /* while */
  147 
  148     return EXIT_SUCCESS;
  149 } /* main */
  150