Zapped a couple of guys [both 8s :-)] with some quick calculation of the DoW (Day of Week) of their DoB (Date of Birth) the last couple of days. Thought it would be a good one to share.
The mental calendar uses a progression of offsets, across years and months, before homing on to the day within the month.
Since a year is 52 weeks plus one day and a leap year with one more extra day, all we have to do is to adjust for the extra days over years.
Across months, we have to remember a small offset table, signifying the cumulative number of days over and above the four weeks that each month has. This table is as follows for a non-leap year: (each row is a quarter)
6 1 4
6 2 5
0 3 5
This offset is for the "zero"th day of the month and the table implies that, if 01.JAN is a Monday, 01.FEB is three days ahead, i.e., a Thursday. Thus, we can define a ZeroDate() function, which is the DoW of the date less one (subtracting the one initially itself makes the calculation simpler and free from errors).
There is no separate table to remember for leap years as the leap day is adjusted in the years itself. The sample calculation later makes that clear.
Within the month, we just have to add the day we are interested in and do a mod 7 (remainder after division by 7) operation. The remainder gives the DoW (1=Monday, 2=Tuesday, …, 6=Saturday, 0=Sunday).
Let's find out the DoW of 18.OCT.1956, the date of birth of Martina Navratilova.
Add the number of years in the year (56) as well as the number of leap years (14, not 13) till 18.OCT.1956. We get 70.
Then, add the offset (0) for October from the month-offset table, to get 70.
To that, add the day (18) to get 88 (70 + 18). Divide that (88) by 7 and get the remainder (4). So Ms. Navratilova was born on a Thursday.
For 19yy dates, we have to add the years in the date. For 20yy dates, we have to add the years in the date less one, i.e., we have to add the number of completed years for 20yy dates. This is because 01.JAN.1901 was a Tuesday, while 01.JAN.2001 was a Monday. The mental calendar absorbs the offsets through this mechanism.
To get the DoW of Katrina (29.AUG.2005), we have the offsets as:
4 (completed years) + 1 (leap year) + 2 (Offset for August) + 29 (Day) = 36
with a mod 7 operation giving 1. So Katrina hit on a Monday.
What's the DoW for 21.DEC.2012, the supposed end of the world as we know it? We have the offsets as:
11 (completed years) + 3 (leap years) + 5 (Offset for December) + 21 (Day) = 40
with a mod 7 operation giving 5. So the world will supposedly end on a Friday or, as Anand observed sometime back, a "Fry"day ;-)
I find the mental calendar useful for doing a "CRC" of the date. While staying at Chennai as a PG in the early 1990s, i had a fellow roomie, who was born the same day as Swami Vivekananda. So i did the DoW of his DoB and said that it was nice that he was a born on a Tuesday, like me. But he said that he was actually born on a Sunday and that's why folks call him Ravi at home.
I noticed that there was a leap year between his given DoB and the actual one and so asked him the inevitable question: "Were you actually born the earlier year?"