Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/1/136/e.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   1000000000
   29 
   30 int sieve[UPPER];
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     int i;
   36     int j;
   37 
   38     sieve[1] = 1;
   39     printf("starting setup\n");
   40     for (i=6; UPPER>i; i++)
   41         {
   42             sieve[i] = -i;
   43         }
   44     printf("sieve set\n");
   45     for (i=4; UPPER>i; i=i+2)
   46         {
   47             sieve[i] = i;
   48         }
   49     printf("multiples of 2 set\n");
   50     for (i=6; UPPER>i; i=i+3)
   51         {
   52             sieve[i] = i;
   53         }
   54     printf("multiples of 3 set\n");
   55     for (i=10; UPPER>i; i=i+5)
   56         {
   57             sieve[i] = i;
   58         }
   59     printf("multiples of 5 set\n");
   60     for (i=7; UPPER>i; i=i+2)
   61         {
   62             /* build rest of prime list */
   63             if (0 > sieve[i])
   64                 {
   65                     /* found a prime */
   66                     printf("Clearing %d\n", i);
   67                     sieve[i] = 0;
   68                     for (j=i; UPPER>j; j=j+i)
   69                         {
   70                             sieve[j] = 0;    /* erase all multiples of i */
   71                         }
   72                 } /* found a prime */
   73         } /* build rest of prime list */
   74 } /* FUNCTION init */
   75 
   76 void process()
   77 {
   78     /* FUNCTION process */
   79     int i;
   80     int cnt;
   81 
   82     for (i=1,cnt=0; cnt<=1500; i++)
   83         {
   84             /* look for non-zero values */
   85             if (0 != sieve[i])
   86                 {
   87                     cnt++;
   88                     printf("(%d) sieve[%d] = %d\n", cnt, i, sieve[i]);
   89                 }
   90         } /* look for non-zero values */
   91 
   92     printf("%d\n", sieve[i]);
   93 } /* FUNCTION process */
   94 
   95 int main ()
   96 {
   97     /* main */
   98     init();
   99     process();
  100     return EXIT_SUCCESS;
  101 } /* main */
  102