Computer Programming Contest Preparation

ToolBox - Source for: 106/10699/fast1.c



/home/toolbox/public_html/solutions/106/10699/fast1.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 (FALSE)
   14 
   15 #define MAX_PRIMES 78505
   16 #define MAX_SIZE 1000005
   17 
   18 /*
   19  *  Author:
   20  *    Date: 2005-07-20
   21  * Purpose: practice
   22  * Problem: 10699
   23  */
   24 
   25 /*
   26  * This template reads data until a terminating value is reached.
   27  */
   28 
   29 int num;
   30 int buff[MAX_SIZE];
   31 
   32 int init()
   33 {
   34     /* FUNCTION init */
   35     int i;
   36     int j;
   37 
   38     for (i=1; MAX_SIZE>i; i++)
   39         {
   40             buff[i] = 0;
   41         }
   42     /* now start adding each factor */
   43     for (i=2; MAX_SIZE>i; i++)
   44         {
   45             /* process each prime number */
   46             if (0 == buff[i])
   47                 {
   48                     /* have a prime! */
   49                     buff[i] = 1;
   50                     for (j=2*i; MAX_SIZE>j; j=j+i)
   51                         {
   52                             /* update each multiple ot include this prime factor */
   53                             buff[j] = buff[j] + 1;
   54                         } /* update each multiple ot include this prime factor */
   55                 } /* have a prime! */
   56         } /* process each prime number */
   57 } /* FUNCTION init */
   58 
   59 int dump()
   60 {
   61     /* FUNCTION dump */
   62 } /* FUNCTION dump */
   63 
   64 int getInput()
   65 {
   66     /* FUNCTION getInput */
   67     int dataReadFlag;
   68 
   69     scanf(" %d ", &num);
   70 
   71     dataReadFlag = (0 != num);
   72     return (dataReadFlag);
   73 } /* FUNCTION getInput */
   74 
   75 void process()
   76 {
   77     /* FUNCTION process */
   78     printf("%d : %d\n", num, buff[num]);
   79 } /* FUNCTION process */
   80 
   81 int main ()
   82 {
   83     /* main */
   84     int moreToDo;
   85 
   86     init();
   87     moreToDo = getInput();
   88     while (moreToDo)
   89         {
   90             /* while */
   91             process();
   92             moreToDo = getInput();
   93         } /* while */
   94 
   95     return EXIT_SUCCESS;
   96 } /* main */
   97