//  LC3.C
    //  Counts the numbers of intercalary years.
    //  Author: Peter Meyer
    //  Last mod.: 1998-02-08
    
    #include <stdio.h>
    #include <conio.h>
    
    #define MAX_SUM 27
    #define NUM_YEARS 1689
    #define M1 2
    #define M2 22
    #define M3 23
    #define N0 30
    #define N1 31
    #define N2 28
    #define N3 29
    #define N4 30
    
    int vals[MAX_SUM+1];
    
    /*-----------*/
    void main(void)
    {
    int i, th, h, t, r, year, n, n0, n1, n2, n3, n4, num_months;
    long num_days=0;
    
    printf("This is a list of all intercalary years in\n"
        "a 1689-year cycle (first year = 1, final year = 1689).\n");
    printf("'#' means that the year is an intercalary year\n"
        "by virtue of being divisible by 3 or by 9.\n");
    printf("'*' means that the year is an intercalary year\n"
        "by virtue of its digits summing to 2, 22 or 23.\n");
    printf("\nPress a key ... \n\n");
    getch();
    
    for ( i=1; i<=MAX_SUM; i++ )
        vals[i] = 0;
    
    n = n0 = n1 = n2 = n3 = n4 = 0;
    
    for ( year=1; year<=NUM_YEARS; year++ )
        {
        if ( !(year%3) )      //  if year divisible by 3
            {
            if ( !(year%9) )  //  if year divisible by 9
                {
                n0++;
                num_days += N0;
                printf("%4d#9   ",year);
                }
            else
                {
                n1++;
                num_days += N1;
                printf("%4d#3   ",year);
                }
            if ( !((++n)%8) )       //  8 per line
                printf("\n");
            }
        else
            {
            //  Sum digits of year.
            i = year;
            th = i/1000;
            i -= th*1000;
            h = i/100;
            i -= h*100;
            t = i/10;
            i -= t*10;
            r = th + h + t + i;
            vals[r]++;
            if ( r == M1 )
                {
                n2++;
                num_days +=  N2;
                }
            else if ( r == M2 )
                {
                n3++;
                num_days += N3;
                }
            else if ( r == M3 )
                {
                n4++;
                num_days += N4;
                }
            if ( r == M1 || r == M2 || r == M3 )
                {
                printf("%4d*%-2d  ",year,r);
                if ( !((++n)%8) )
                    printf("\n");
                }
            }
        }
    
    printf("\n\n");
    for ( i=1; i<=MAX_SUM; i++ )
        {
        printf(" %2d:%-3d  ",i,vals[i]);
        if ( !(i%8) )
            printf("\n");
        }
    
    printf("\n\nn0=%d, n1=%d, n0+n1=%d\n"
           "n2=%d, n3=%d, n4=%d, n2+n3+n4=%d\n"
           "n0+n1+n2+n3+n4=%d",
            n0,n1,n0+n1,n2,n3,n4,n2+n3+n4,n0+n1+n2+n3+n4);
    
    num_months = 12*NUM_YEARS + n0 + n1 + n2 + n3 + n4;
    
    num_days += NUM_YEARS*354L;  //  354 = number of days in twelve 29/30-day months.
    
    printf("\n\nIn a %d-year cycle there are %d calendar months and %ld days.",
        NUM_YEARS,num_months,num_days);
    printf("\nThus the average length of a calendar month is %.7f days,",
        (double)num_days/num_months);
    printf("\nand the average length of a calendar year is %.4f days.\n",
        (double)num_days/NUM_YEARS);
    }