Computer Programming Contest Preparation

ToolBox - Source for: 1/136/d.c



/home/toolbox/public_html/solutions/1/136/d.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 (TRUE)
   14 
   15 #define UPPER    2000000
   16 /*            2147483648 */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2014-10-29
   21  * Purpose: fun
   22  * Problem:136 - Ugly Numbers
   23  */
   24 
   25 /*
   26  * This template reads data until a terminating value is reached.
   27  */
   28 
   29 int twos[100];
   30 int threes[100];
   31 int fives[100];
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     int i;
   37     int t;
   38 
   39     twos[0] = 1;
   40     for (i=1,t=2; t<(2*UPPER); i++, t=t*2)
   41         {
   42             twos[i] = t;
   43         };
   44     threes[0] = 1;
   45     for (i=1,t=3; t<(3*UPPER); i++, t=t*3)
   46         {
   47             threes[i] = t;
   48         };
   49     fives[0] = 1;
   50     for (i=1,t=5; t<(5*UPPER); i++, t=t*5)
   51         {
   52             fives[i] = t;
   53         };
   54 } /* FUNCTION init */
   55 
   56 void process()
   57 {
   58     /* FUNCTION process */
   59     int i;
   60     int j;
   61     int k;
   62     int t;
   63     int cnt = 0;
   64     char a[UPPER];
   65 
   66     for (i=0; UPPER>i; i++)
   67         {
   68             a[i] = 0;
   69         }
   70 
   71     a[1] = 1;
   72     for (i=0; UPPER>twos[i]; i++)
   73         {
   74             /* for powers of 2 */
   75             for (j=0; UPPER>(threes[j]*twos[i]); j++)
   76                 {
   77                     /* for powers of 3 */
   78                     for (k=0; UPPER>(fives[k]*threes[j]*twos[i]); k++)
   79                         {
   80                             /* for powers of 5 */
   81                             t = twos[i] * threes[j] * fives[k];
   82                             if (UPPER > t)
   83                                 {
   84                                     a[t] = 1;
   85                                     DEBUG printf("setting a[t] -- i=%d 2=%d   j=%d 3=%d  k=%d 5=%d t=%d\n", i, twos[i], j, threes[j], k, fives[k], t);
   86                                 }
   87                         } /* for powers of 5 */
   88                 } /* for powers of 3 */
   89         } /* for powers of 2 */
   90 
   91     DEBUG printf("Starting to count\n");
   92     for (i=1; 1500>cnt; i++)
   93         {
   94             if (0 != a[i])
   95                 {
   96                     cnt++;
   97                     printf("%d - %d\n", cnt, i);
   98                 }
   99         }
  100     printf("%d\n", i);
  101 } /* FUNCTION process */
  102 
  103 int main ()
  104 {
  105     /* main */
  106     init();
  107     process();
  108     return EXIT_SUCCESS;
  109 } /* main */
  110