Computer Programming Contest Preparation

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



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