|
Bonus: here's how to compute the day of the year from month/day. First I'll show how independent of the Doomsday stuff, and then show how you can use the Doomsday month number stuff to reduce memorization for the day of year stuff. The basic idea is to compute the year day for the "0th" day of the month, then add the day of the month. For example, today, October 13th is the 286th day of the year, because October 0th is the 273rd day of the day, so 13 days past that is the 286th. The simplest way, short of memorizing the 0th day of each month directly (or the 1st day...if you are going to take this approach might as well do the 1st, not the 0th), is to memorize how far off the actual 0th day is from what the 0th would be if all months were exactly 30 days. Those differences are, for the 12 months: 0, -1, 1, 0, 0, 1, 1, 2, 2, 3, 4, 4
If F[m] refers to the the m'th entry in that list, 1 <= m <= 12, then the 0th day for month m is 30(m-1) + F[m]. E.g., for October, m = 10, so 30x(10-1) + F[10] = 30x9+3 = 273.If it is a leap year, add one more day for dates after February 29. The F table has enough structure that it is not unreasonable to just memorize it. For m >= 4, F[m] = (6x(m-4))//10. If you round toward -∞ instead of 0 when dividing, that works for m >= 3. It's probably harder to remember this than to just remember the table. Let M[m] = the month factor from the Doomsday algorithm: 3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12
Then F[m] = -(M[m] + 2 x m + 2) mod 7. Just remember that -1 <= F[m] <= 4, so you can pick the right range when reducing mod 7.For example, let's do July 20th, 1969, the day of the first Moon landing. July is the 7th month, and the month factor for that in the Doomsday algorithm is 11, so we have F[7] = -(11 + 2x7 + 2) = 1 mod 7, so F[m] = 1, and July 0th is 30x(7-1)+1 = 181, and so July 20th is the 201st day of the year. BTW, if you find F[] easier to memorize than M[], you reverse the formula given above for computing F from M, to go the other way. M[m] = -(F[m] + 2 x m + 2) mod 7. While I'm here, one more Doomsday observation. If a given year, Y, has year number N, then the next year with the same year number is: • Y+6 if Y%4 == 0 or 1 • Y+11 if Y%4 == 2 • Y+5 if Y%4 == 3 (but only if that does not cross a century boundary). The day of weeks within a century go on a 28 year cycle. Within any 28 consecutive years in a century, each of the 7 possible year number values occurs 4 times, once on a year with Y%4 == 0, once on a Y%4 == 1 year, and so on. If you start with Y%4 == 0 year, the gaps between consecutive years with the same year number are 6, 11, 6, 5, which brings you back to the Y%4 == 0 year for the next 28 year cycle. You visit the years in the order 0, 2, 1, 3 mod 4 stepping this way. If you memorize this pattern, it is easier to answer questions about when years or dates with specific properties happen. For example, let's say you want to know when there will next be a Friday the 13th in October. Assuming that there will be another one this century, so using the century factor of 2 (for 20xx), you can work out that in this century Friday October 13 occurs when the year number is 0. Such a year was 2000, and 00 is a Y%4 == 0 year, so Friday October 13th occurs in 2000, 2000+6=2006, 2006+11=2017, 2017+6=2023, and then 2023+5 brings is to the 2028, the next 28 year cycle. So the next Friday October 13 is 2023. BTW, the first Y in a century with year number N and with Y%4 == 0 is (3xN%7)x4. If you don't care about starting from a Y%4 == 0 year, then it is easiest probably to just memorize the first year with year number 0, 1, 2, ..., 6 in a century: 0, 1, 2, 3, 9, 4, 5
and then add multiples of 28 to get to where you want to be. E.g., if you were trying to find the first time your birthday falls on a Thursday after 2040, and figured out you need year number 4, that table would tell you 2009 is such a year, and the 28 year cycles would tell you 2037, 2065, and 2093 are other such years, so you'd probably go to 2037 and work forward from there. 36 is a 1%4 year, so the next with the same year number is 2043, and you are done! |