The Doomsday algorithm, devised by mathematician J. H. Conway, computes the day of the week any given date fell on. The algorithm is designed to be simple enough to memorize and use for mental calculation.
Example. With the algorithm, we can compute that July 4, 1776 (the day the United States declared independence from Great Britain) was a Thursday.
The algorithm is based on the fact that for any year, several dates always fall on the same day of the week, called the doomsday for the year. These dates include 4/4, 6/6, 8/8, 10/10, and 12/12.
Example. The doomsday for 2016 is Monday, so in 2016 the dates above all fell on Mondays. The doomsday for 2017 is Tuesday, so in 2017 the dates above will all fall on Tuesdays.
The doomsday algorithm has three major steps:
Each step is explained in detail below.
The doomsday for the first year in a century is called the anchor day for that century. The anchor day is needed to compute the doomsday for any other year in that century. The anchor day for a century $c$ can be computed with the formula: $$ a = \bigl( 5 (c \bmod 4) + 2 \bigr) \bmod 7 $$ The result $a$ corresponds to a day of the week, starting with $0$ for Sunday and ending with $6$ for Saturday.
Note. The modulo operation $(x \bmod y)$ finds the remainder after dividing $x$ by $y$. For instance, $12 \bmod 3 = 0$ since the remainder after dividing $12$ by $3$ is $0$. Similarly, $11 \bmod 7 = 4$, since the remainder after dividing $11$ by $7$ is $4$.
Example. Suppose the target year is 1954, so the century is $c = 19$. Plugging this into the formula gives $$a = \bigl( 5 (19 \bmod 4) + 2 \bigr) \bmod 7 = \bigl( 5(3) + 2 \bigr) \bmod 7 = 3.$$ In other words, the anchor day for 1900-1999 is Wednesday, which is also the doomsday for 1900.
Exercise 1.1. Write a function that accepts a year as input and computes the anchor day for that year's century. The modulo operator %
and functions in the math
module may be useful. Document your function with a docstring and test your function for a few different years. Do this in a new cell below this one.
import math
def anchor(year):
"""
Take in year and output anchor day
Arguments:
year (int): Any year
Returns:
anc_day (int): Anchor day for given year
"""
year = str(year).zfill(4)
century = int(year[0:2])
anc_day = (5 * (century % 4) + 2) % 7
return anc_day
print(anchor(2015))
print(anchor(201))
print(anchor(2))
print(anchor(1994))
Once the anchor day is known, let $y$ be the last two digits of the target year. Then the doomsday for the target year can be computed with the formula: $$d = \left(y + \left\lfloor\frac{y}{4}\right\rfloor + a\right) \bmod 7$$ The result $d$ corresponds to a day of the week.
Note. The floor operation $\lfloor x \rfloor$ rounds $x$ down to the nearest integer. For instance, $\lfloor 3.1 \rfloor = 3$ and $\lfloor 3.8 \rfloor = 3$.
Example. Again suppose the target year is 1954. Then the anchor day is $a = 3$, and $y = 54$, so the formula gives $$ d = \left(54 + \left\lfloor\frac{54}{4}\right\rfloor + 3\right) \bmod 7 = (54 + 13 + 3) \bmod 7 = 0. $$ Thus the doomsday for 1954 is Sunday.
Exercise 1.2. Write a function that accepts a year as input and computes the doomsday for that year. Your function may need to call the function you wrote in exercise 1.1. Make sure to document and test your function.
import math
def dooms(year):
"""
Take in year and output doomsday
Arguments:
year (int): Any year
Returns:
doomsday (int): Doomsday for given year
"""
ancday = anchor(year)
ltwo = int(str(year)[len(str(year))-2:len(str(year))])
doomsday = (ltwo + int(math.floor(ltwo / 4)) + ancday) % 7
return doomsday
print(dooms(1954))
print(dooms(1978))
print(dooms(2016))
print(dooms(2017))
The final step in the Doomsday algorithm is to count the number of days between the target date and a nearby doomsday, modulo 7. This gives the day of the week.
Every month has at least one doomsday:
Example. Suppose we want to find the day of the week for 7/21/1954. The doomsday for 1954 is Sunday, and a nearby doomsday is 7/11. There are 10 days in July between 7/11 and 7/21. Since $10 \bmod 7 = 3$, the date 7/21/1954 falls 3 days after a Sunday, on a Wednesday.
Exercise 1.3. Write a function to determine the day of the week for a given day, month, and year. Be careful of leap years! Your function should return a string such as "Thursday" rather than a number. As usual, document and test your code.
import math
def dayofweek(day, month, year):
"""
Take in day, month, year and output which day of the week
Arguments:
day (int): Any day (1-31)
month (int): Any month (1-12)
year (int): Any year
Returns:
string: A day of the week
"""
allday = ('Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
monthdict = {'1':10, '2':28, '3':21, '4':4, '5':9, '6':6, '7':11, '8':8, '9':5, '10':10, '11':7, '12':12}
doomsday = dooms(year)
if((year % 4) == 0 and (month == 1 or month ==2)):
day = day - 1
day = (day - monthdict[str(month)] + doomsday) % 7
return allday[day]
dayofweek(28, 2, 2016)
Exercise 1.4. How many times did Friday the 13th occur in the years 1900-1999? Does this number seem to be similar to other centuries?
count = 0
for i in xrange(1900,1999,1):
for j in xrange(1, 13, 1):
friday = dayofweek(13, j, i)
if(friday == "Friday"): count = count + 1
count
The amount of times Friday the 13th occured from the 17th to 22th centuries was within a range from 169 to 172. In the 20th century, it happened 172 times, which does not differ greatly from previous and future centuries.
Exercise 1.5. How many times did Friday the 13th occur between the year 2000 and today?
count = 0
for i in xrange(2000,2017,1):
for j in xrange(1, 13, 1):
friday = dayofweek(13, j, i)
if(friday == "Friday"): count = count + 1
if (dayofweek(13, 1, 2017) == "Friday"): count = count + 1
count
Friday the 13th happened 30 times during 2000 and today
Exercise 2.1. The file birthdays.txt
contains the number of births in the United States for each day in 1978. Inspect the file to determine the format. Note that columns are separated by the tab character, which can be entered in Python as \t
. Write a function that uses iterators and list comprehensions with the string methods split()
and strip()
to convert each line of data to the list format
[month, day, year, count]
The elements of this list should be integers, not strings. The function read_birthdays
provided below will help you load the file.
import re
def read_birthdays(file_path):
"""Read the contents of the birthdays file into a string.
Arguments:
file_path (string): The path to the birthdays file.
Returns:
list: The contents of the birthdays file in integers
"""
with open(file_path) as f:
birthdays = f.readlines()[6:371]
listbirth = []
for i in birthdays:
i = i.strip()
listbirth.append(map(int,re.split(r'[/\t]',i)))
return listbirth
read_birthdays("birthdays.txt")
Exercise 2.2. Which month had the most births in 1978? Which day of the week had the most births? Which day of the week had the fewest? What conclusions can you draw? You may find the Counter
class in the collections
module useful.
births = read_birthdays("birthdays.txt")
best = 0
current = 0
count2 = 0
for i in births:
if(current == i[0]):
count = count + i[3]
else:
count = i[3]
current = i[0]
if(count > count2):
best = current
count2 = count
best
August had the most births in 1978.
dayofweek(1, 1, 1978)
days = [0, 0, 0, 0, 0, 0, 0]
for i in xrange(0,(len(births)-1),1):
days[i % 7] = days[i % 7] + births[i][3]
days
Friday had the most births of any day. Sunday had the least births of any day.
It is hard to come to many conclusions, but perhaps some hospitals are close on Saturdays and Sundays, which cold deflate the numbers for those particular days. It looks as though the numbers during the weekdays are similar, and the variance could be due to chance.
August having the most births of any month could be due to random chance, but there is slight possibility that because families are together the most during December, that could lead to more attempts to have a baby during that time of the year. Giving 9 months for pregnancy, the baby would be born in August.
Exercise 2.3. What would be an effective way to present the information in exercise 2.2? You don't need to write any code for this exercise, just discuss what you would do.
For the first question, I would make a bar graph of the months by how many babies were born each month in order to illustrate the data accurately. For the second and third question, I would make a bar graph of the days of the week by how many babies were born each day for the same reason.