Doomsday algorithm
|
The Doomsday algorithm is a way of calculating the day of the week of a given date. It is perpetually accurate since the Gregorian calendar moves in cycles of 400 years. It makes use of the fact that, in each year, certain dates are all on the same day of the week.
The algorithm was invented by John Horton Conway. It can be used for either the Gregorian Calendar or the Julian Calendar, but note that Julian calendar Doomsdays usually occur on different days from the Gregorian calendar Doomsdays.
Contents |
The algorithm
The algorithm has three steps, namely, finding the anchor day for the century, finding a year's doomsday, and finding the day of week of the day in question.
To find a century's anchor day, begin by finding the century which the date falls in. (For the purposes of this calculation, century years will be treated as though they fall in the century that follows, even though they technically fall in the same century as the years before them. 2000 is therefore part of the twenty-first century, not the twentieth century.) A year's century number is equal to its first two digits plus one (so 1966 is in the twentieth century). Take the century number and multiply by 5. Separately, take the integral part of the quotient when the century number minus one is divided by four. Add these two numbers (the product of the multiplication and the integral of the division). Taking the remainder mod 7 is often done at this point to make the numbers more manageable, since it won't affect the result. Now count forward that number of days from Thursday to get the anchor day for the century. For example, the calculation for the twentieth century is: multiply the century number 20 by 5 to get 100; subtract 1 from the century number and then divide by 4 to get 4; sum 100 and 4 to get 104; find the remainder mod 7 to get 6; count 6 days on from Thursday for the answer, Wednesday. Similarly, the anchor date for the twenty-first century is Tuesday.
Next, one must find the year's Doomsday. To accomplish that according to Conway, begin by taking the integral part of the quotient when the year's last two digits are divided by 12. Next, determine the remainder of this first quotient. After that, take the integral part of this number when it is divided by 4. Finally, determine the sum of the three numbers. (This is equivalent mod 7, as it must be, to the sum of the last two digits plus those digits divided by four.)
Now count forward the specified number of days from the anchor day (again taking remainder mod 7 can be done) to get the year's Doomsday.
The following days all occur on Doomsday for any given Gregorian year:
- January 31 (or "January 32", ie February 1, in leap years)
- February 28 (or February 29 if it's a leap year)
- "March 0" (a backwards extension of the calendar, equivalent to the last day of February.)
- April 4
- May 9
- June 6
- July 11
- August 8
- September 5
- October 10
- November 7
- December 12
The dates listed above were chosen to be easy to remember; the ones for even months are simply doubles, 4/4, 6/6, 8/8, 10/10, and 12/12. Four of the odd month dates (5/9, 9/5, 7/11, and 11/7) are based on the phrase "I work from 9 to 5 at the 7-11."
For dates in March, March 7 falls on Doomsday, but the pseudodate "March 0" is easier to remember, as it is necessarily the same as the last day of February. Similarly, in leap years the pseudodate "January 32" is easier to remember than January 4, as it is necessarily the same as February 1.
Therefore, if you know what day of the week Doomsday — the last day in February — is for a given year, you can easily determine the day of the week for any other date in that year, by finding the nearest Doomsday.
In code
The following is the algorithm in C. Please note that division in C (using the / operator) is integer division and that the % operator is the modulo operator.
Set "year" to whatever year (this case is 2005) computation is desired and the numeric values of the days of the week are:
- 0 – Sunday
- 1 – Monday
- 2 – Tuesday
- 3 – Wednesday
- 4 – Thursday
- 5 – Friday
- 6 – Saturday
// Determine the anchor day for the century year = 2005; century = year/100 + 1; a = century * 5; b = (century - 1) / 4; c = a + b; anchor = (c + 4) % 7; // Determine the Doomsday for the year in question e = (year % 100)/12; f = (year % 100)%12; g = f/4; h = e + f + g; dday = (h + anchor) % 7;
"anchor" is the anchor day for the century and "dday" is the doomsday for the year. If "dday" is found to be Monday, then January 3 (or 4th), February 28 (or 29th), March 0, April 4, May 9, etc. (see above). To find the day of the date in question, determine the number of days away from the doomsday for that month and add it to "dday" modulo 7.
Examples
Example 1 (this year)
Suppose you want to know which day of the week Christmas Day of 2005 is. In the year 2005, Doomsday is Monday. (The century's anchor day is 110 days after Thursday, or Tuesday, and 2005's Doomsday is six days beyond.) This means that December 12 is a Monday. December 25, being thirteen days afterwards, falls on a Sunday.
Example 2 (other years of this century)
Suppose that you want to find the day of week that the September 11, 2001 attacks on the World Trade Center occurred. The anchor is Tuesday, and one day beyond is Wednesday. September 5 is a Doomsday, and September 11, six days later, falls on a Tuesday.
Example 3 (other centuries)
Suppose that you want to find the day of week that the American Civil War broke out at Fort Sumter, which was April 12, 1861. The anchor day is 99 days after Thursday, or Friday. The digits 61 give a displacement of six days, so Doomsday was Thursday. Therefore, April 4 was Thursday, so April 12, eight days later, is a Friday.
External links
- What is the day of the week, given any date? (http://quasar.as.utexas.edu/BillInfo/doomsday.html)
- Doomsday Algorithm (http://rudy.ca/doomsday.html)ko:둠스데이 알고리즘