Bad, vile and meaningless: Save That Calendar from Alan's clob

Calendar reusability

Here's a short perl program that figures out what years you could reuse your calendar:

my $year = shift;
for (1900 .. 2099) {
    my @out = `cal $_`;
    shift(@out);
    push @{$tmp{"@out"}}, $_;
}
foreach my $x (values %tmp) {
    print "@$x" if grep { $_ == $year } @$x
}

Save that into a file, let's call it calendar.pl, and run it:

% calendar.pl 2005
1910 1921 1927 1938 1949 1955 1966 1977 1983 1994 2005 2011 2022
2033 2039 2050 2061 2067 2078 2089 2095

So if you had saved your calendar from 1994, you could have reused it this year. Or if you keep this year's calendar, you get to use it already 6 years from now, on 2011!

Another potentially interesting question may be how many unique calendars there are. The answer is simple, considering that you have 4-year leap cycle and 7 days per week and 365 days per year. Since 365 is 52 weeks and 1 day, it follows that on non-leap years the calendar shifts forward by one day. That way you get 7 unique calendars before the cycle repeats.

However, at leap years February is different (and the calendar will shift forward by 2 days). Since leap years are introduced basically every 4 years, and 7 divided by 4 leaves a remainder, it follows that all the 7 different calendars must get a variant where February is longer, hence there are in total 14 different calendars but surprisingly, their distribution is a bit odd: some of them are much more popular than others.

The mystery is not at all complicated. If we consider yesteryear, the program shows us this:

% ./calendar.pl 2004
1920 1948 1976 2004 2032 2060 2088

There is a 28-year cycle (which is 7 * 4). This follows because firstly all leap year calendars are special -- they have February the 29th. The leap year cycle simply evenly distributes the weekdays making a cycle that repeats in full every 28 years. So, in nutshell, saving the leap year calendars is questionable, since they're pretty antique by the time you put them on walls again. To help dissolve the final remains of the mystery, here's a program:

use Time::Local;
for (1976 .. 2003) {
print $_ . ": "
    . (gmtime(timegm(0, 0, 0, 1, 0, $_)))[6]
    . ($_ % 4 == 0 ? "*" : "")
}

This one gives us the first day of each year, and whether it's leap year or not -- together these two facts suffice to uniquely identify which calendar the entire year must follow (Sunday is the 0th day of the week in this list):

1976: 4*
1977: 6
1978: 0
1979: 1
1980: 2*
1981: 4
1982: 5
1983: 6
1984: 0*
1985: 2
1986: 3
1987: 4
1988: 5*
1989: 0
1990: 1
1991: 2
1992: 3*
1993: 5
1994: 6
1995: 0
1996: 1*
1997: 3
1998: 4
1999: 5
2000: 6*
2001: 1
2002: 2
2003: 3

Now observe the steady progression of the days in each leap year: 4, 2, 0, 5, 3, 1, 6, repeat. Hence the 28 year cycle. It'll go unperturbed like this until 2100 that has no leap year. The other non-leap year weekdays reappear in steady 11, 11, 6 year cycles whose structure can be quite plainly analyzed from the above listing.

The lack of leap year in 2000 caused this stretch of time to be rather regular. In more normal conditions, the year 2000 would have bumped the cycle off one day's worth and produced more interesting numbers than 28, 11 and 6. Alas, that was not to be. So, the next time you hear someone proclaim that we live in interesting times, you shall know exactly why he is not right at all.