Sie sind auf Seite 1von 14

The Doomsday Algorithm

Run for
your life.
The Doomsday Algorithm: Origins
The mathematician Dr. John Horton
Conway invented the algorithm and first
published it in 1982 in his book Winning
Ways For Your Mathematical Plays
The Doomsday Algorithm determines what
day of the week any given date falls on.
The Doomsday Algorithm: Origins
Why was the algorithm invented?
Because Dr. Conway was a huge nerd.
But so are you if youre in this class.
Nerd
So today, youre going to learn how to use
it to impress friends and family alike at
parties, bar-mitzvahs, and jury duty.

Why the Doomsday Algorithm?
Because it sounds cool.
Doomsday is just the arbitrary way that
we refer to the special day of the week
assigned to a year that we can use to
track down the day of the week that a
specified date falls on during that year
For instance, the Doomsday of 2005 is
Monday
Why Monday?
The Doomsday of any year is the day of
the week that the last day of February falls
on (either the 28
th
or 29
th
, depending on
whether its leapyear or not)
February 28
th
of 2005 is a Monday, so
2005s Doomsday is also a Monday
Dont worry. This is going somewhere.
What can you do with a
Doomsday?
Once we know the Doomsday of a given
year, we can determine what dates are
guaranteed to also be Doomsdays in each
month.
We already know the last day of February
is a Doomsday. Now how do we do the
other months?
The other months
January 3
rd
always falls on a Doomsday, except
on leapyears, when it falls on January 4
th
March 7
th
is always a Doomsday
Even-numbered months have Doomsdays that
fall on the dates corresponding to the month
number (October 10
th
, December 12
th
)
For odd-numbered months, remember the
phrase I work 9-5 a the 7-11. Doomsday falls
on the 9
th
day of the 5
th
month and vice-versa,
and the 7
th
day of the 11
th
month and vice-versa.
Finding a date in the queried month
So now we know how to determine the
starting point of searching through a
month for the day of the week that the
queried date falls on.
In 2005, we know that the following dates
are Mondays
1. January
3
rd
(Its always the 3
rd
on non-leap years)
2. February
28
th
(Its always the last day of February)
3. March
7
th
(Its always on the 7
th
)
4. April
4
th
(4 / 4 / 2005)
5. May
9
th
(9-5 at the 7-11)
6. June
6
th
(6 / 6 / 2005)
7. July
11
th
(9-5 at the 7-11)
8. August
8
th
(8 / 8 / 2005)
9. September
5
th
(9-5 at the 7-11)
10. October
10
th
(10 / 10 / 2005)
11. November
7
th
(9-5 at the 7-11)
12. December
12
th
(12 / 12 / 2005)
Pop Quiz: The Months of 2005
One last step
So lets say were trying to figure out on which
day Halloween falls this year.
We know that October 10
th
is a Monday.
Three weeks later (October 31
st
) its still a Monday.
How about this Christmas?
December 12
th
is a Monday, and we have to go 13
days forward to reach the 25
th
.
One week and six days ahead of a Monday, or
Six days after Monday, or
One day before Monday, or
Sunday


What about other years?
Doomsday is advanced by one each successive year, and twice for
leapyears.
Heres a formula to calculate the Doomsday of any year, starting at
1900:
Step 1: The number of 12's in the year, minus 1900, or ((year
1900) / 12)
Step 2: The remainder of step 1, or (year 1900) % 12)
Step 3: the number of 4's in step 2, or (Step2 / 4)
Step 4: Add the above three results (Step1 + Step2 + Step3)
Step 5: Subtract the 7s from the sum (Step4 % 7)
What youre left with is the difference in days from Wednesday, the
Doomsday of 1900.
So the Doomsday of a given year is Step5 days after Wednesday


Now lets do some birthdays
The technical stuff
The Doomsday Algorithm has a runtime of
O(1)


A function to use the Doomsday Algorithm
to take a date and return a number from 0
to 6, representing the day of the week on
which it falls would have the following
code

doomsday($q_month, $q_date, $q_year) {
// Discern whether the queried year is a leapyear or not
if($q_year % 4 == 0)
$leapyear = 1;
else
$leapyear = 0;

// Determine the Doomsday of the given year
$step_1 = floor(($q_year - 1900) / 12);
$step_2 = ($q_year - 1900) % 12;
$step_3 = floor($step_2 / 4);
$sum = $step_1 + $step_2 + $step_3;
$doomsday = ($sum + 3) % 7;

//Determine what date is a Doomsday for a given month
if($q_month == 2) //February
$date_of_doomsday = 28 + $leapyear;
elseif($q_month % 2 == 0) //Even-numbered months
$date_of_doomsday = $q_month;
elseif($q_month == 1) //January
$date_of_doomsday = 3 + $leapyear;
elseif($q_month == 3) //March
$date_of_doomsday = 7;
elseif($q_month == 9) //September
$date_of_doomsday = 5;
elseif($q_month == 5) //May
$date_of_doomsday = 9;
elseif($q_month == 7) //July
$date_of_doomsday = 11;
elseif($q_month == 11) //November
$date_of_doomsday = 7;

//Implement offset
$result_day = ($doomsday + ($q_month - $date_of_doomsday)) % 7;

//Returns 0 for Sunday, 1 for Monday, up to 6 for Saturday
return $result_day;
}

Das könnte Ihnen auch gefallen