Computer Programming Contest Preparation

ToolBox - Source for: 12/1210/a.c



/home/toolbox/public_html/solutions/12/1210/a.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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2022-10-25
   19  * Purpose: fun
   20  * Problem: 1210 - Sum of Consecutive Prime Numbers
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_PRIMES 1250
   28 #define MAX_SIZE 10005
   29 
   30 int primes[MAX_PRIMES];
   31 int buff[MAX_SIZE];
   32 int numPrimes;
   33 int num;
   34 
   35 
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40     int i;
   41     int j;
   42     int total;
   43     int next;
   44 
   45     numPrimes = 1;
   46     primes[0] = 2;
   47 
   48     /* sieve of erasthones */
   49     for (i=0,j=3; i<(MAX_SIZE/2); i++,j=j+2)
   50         {
   51             buff[i]=j;
   52         }
   53     for (i=0; i<(MAX_SIZE/2); i++)
   54         {
   55             /* loop through primes */
   56             if (0 != buff[i])
   57                 {
   58                     /* found a prime */
   59                     DEBUG printf("Adding prime %d : %d\n", numPrimes, buff[i]);
   60                     primes[numPrimes] = buff[i];
   61                     numPrimes++;
   62                     for (j=i+buff[i]; j<(MAX_SIZE/2); j=j+buff[i])
   63                         {
   64                             /* mark out multiples */
   65                             buff[j] = 0;
   66                         } /* mark out multiples */
   67                 } /* found a prime */
   68         } /* loop through primes */
   69     primes[numPrimes] = 0;
   70     /* empty out buf for next step */
   71     for (i=0; i<MAX_SIZE; i++)
   72         {
   73             buff[i]=0;
   74         }
   75     /* now set up buf so that each item is the number of ways to get that numer */
   76     for (i=0; numPrimes>i; i++)
   77         {
   78             /* start with each possible prime */
   79             total = primes[i];
   80             next = i+1;
   81             while ((MAX_SIZE>total) && (numPrimes>=next))
   82                 {
   83                     /* add up sums */
   84                     buff[total] = buff[total] + 1;
   85                     total = total + primes[next];
   86                     next++;
   87                 } /* add up sums */
   88         } /* start with each possible prime */
   89 } /* FUNCTION init */
   90 
   91 void dump()
   92 {
   93     /* FUNCTION dump */
   94     int i;
   95 
   96     for (i=0; MAX_SIZE>i; i++)
   97         {
   98             /* for */
   99             printf("%4d: %d\n", i, buff[i]);
  100         } /* for */
  101 } /* FUNCTION dump */
  102 
  103 int getInput()
  104 {
  105     /* FUNCTION getInput */
  106     int dataReadFlag;
  107 
  108     scanf(" %d ", &num);
  109     dataReadFlag = (0 < num);
  110     return (dataReadFlag);
  111 } /* FUNCTION getInput */
  112 
  113 void process()
  114 {
  115     /* FUNCTION process */
  116     printf("%d\n", buff[num]);
  117 } /* FUNCTION process */
  118 
  119 int main()
  120 {
  121     /* main */
  122     int moreToDo;
  123 
  124     init();
  125     DEBUG printf("(numPrimes %d)\n", numPrimes);
  126     moreToDo = getInput();
  127     while (moreToDo)
  128         {
  129             /* while */
  130             process();
  131             moreToDo = getInput();
  132         } /* while */
  133 
  134     return EXIT_SUCCESS;
  135 } /* main */
  136