## Tuesday, September 30, 2014

### Calendars

A company at which I once worked had to solve the "Y2K problem" where comparing year 99 with year 00 gives you the wrong result.  The comparison yields "99 is greater (later) than 00" and that's wrong because it's really [19]99 vs [20]00.  The solution they implemented was to subtract 28 from every year they had to process so that "99 v 00" became "71 v 72" which yields the correct result: '71 is smaller (earlier) than 72".  Why 28?  Because every 28 years the calendar repeats itself and days-of-the-week exactly match date-of-the-year.

Of course, this only works because 2000 was a leap year.  "Of course, 2000 was a leap year!" I hear you exclaim. "It's evenly divisible by 4!"  Well, actually, no, that's not the whole rule.  A year is a leap year if it's evenly divisble by 4, but not if it's evenly divisible by 100 — unless it's also evenly divisible by 400.  1900 was not a leap year, and 2100 won't be, either.  Wait;  you'll see.  1600 was a leap year, and 2400 will be, too, although I doubt very much that you care.

This got me to thinking about why our calendars repeat, and why every 28 years and not every 14 years.  Here's why:

There are only 14 unique yearly calendars.  There are seven for "January 1st is sUnday, Monday, Tuesday, Wednesday, thuRsday, Friday, and sAturday.  We'll call these calendars U, M, T, W, R, F, and A.  There are seven more for "this is a leap year"; we'll call those U*, M*, T*, etc.

If a year were 364 days long (52 x 7), every year would begin on the same day of the week.  Since the typical year is 365 days long, succeeding years begin on succeeding days of the week: if this year began on a Tuesday, next year will begin on Wednesday.  When it's a leap year (366 days), next year will skip a day;  instead of beginning on Wednesday, it will begin on thuRsday.  Every 4th year will be a leap year.  So, let's see how they line up.

Let's assume a starting point where January 1st is sUnday, and it's a leap year.  The first calendar is U* and it's 366 days long.  The next calendars will be T, W, R, and F*.  Next is U, M, T, and W*, then F, A, U, and M*, then W, R, F, and A*, then M, T, W, and R*, then A, U, M,and T*, then R, F, A, and U*.  We are now back where we started, at a calendar with January 1st on sUnday, and it's a leap year.  Let's see how we got there.

 U M T W R F A U* T W R F* U M T W* F A U M* W R F A* M T W R* A U M T* R F A U*

In 28 years, each day of the week gets to be associated with 'starting a leap year' once and 'not starting a leap year' thrice.  Neat, but it only works if every 4th year is leap.  As long as you don't cross a non-leap-century-boundary (and that only happens three times every four hundred years) you're okay.

While we're on the topic...  Every once in a while I see some nonsense either on FaceBook or via e-mail that says something like "This May has five Fridays and five Saturdays and five Sundays! That only happens once every 42 bazillion years!"  Well, as a matter of fact, no.  It happens four times every 28 years and now you know why.