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(x):
cen = int(math.floor(x/100))
a = (5 * (cen % 4) + 2) % 7
"""Takes a year argument, divides this by 100 and rounds down and is converted to int type.
Formula used to calculate day of week for first doomsday.
"""
return a
anchor(1954)
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 doom (x):
y = x % 100
d = (y + int(math.floor(y/4)) + anchor(x)) % 7
"""Takes a year argument, divides this by 100 and gets remainder.
Uses formula to obtain day of week of doomsday on that year
"""
return d
doom(1954)
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.
def day(month, day, year):
"""Assigns the year as leap or regular
"""
if year % 4 == 0:
type = "leap"
else:
type = "regular"
if type == "leap":
Jan = 11
Feb = 29
else:
Jan = 10
Feb = 28
"""Assigns all doomsday dates to leap year or regular
"""
Mar = 21
Apr = 4
May = 9
Jun = 6
Jul = 11
Aug = 8
Sep = 5
Oct = 10
Nov = 7
Dec = 12
"""Find Doomsday, count the number of days before or after doomsday and return its respective day
"""
x = doom(year)
if month == 1:
y = (day - Jan) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 2:
y = (day - Feb) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 3:
y = (day - Mar) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 4:
y = (day - Apr) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 5:
y = (day - May) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 6:
y = (day - Jun) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 7:
y = (day - Jul) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 8:
y = (day - Aug) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 9:
y = (day - Sep) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 10:
y = (day - Oct) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 11:
y = (day - Nov) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
elif month == 12:
y = (day - Dec) % 7
z = (x + y) % 7
if z == 0:
return "Sunday"
elif z == 1:
return "Monday"
elif z == 2:
return "Tuesday"
elif z == 3:
return "Wednesday"
elif z == 4:
return "Thursday"
elif z == 5:
return "Friday"
else:
return "Saturday"
else:
return
day(2, 26, 2015)
day(12, 4, 2000)
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 range(1900, 2000):
for j in range (1, 13):
x = day(j, 13, i)
if x == "Friday":
count = count + 1
count
Exercise 1.5. How many times did Friday the 13th occur between the year 2000 and today?
count = 0
for i in range(2000, 2016):
for j in range (1, 13):
x = day(j, 13, i)
if x == "Friday":
count = count + 1
y = day(1, 21, 2016)
if y == "Friday":
count = count + 1
count
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.
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:
string: The contents of the birthdays file.
"""
with open(file_path) as file:
return file.read()
string = read_birthdays("birthdays.txt")
string
new = string.split("\n")
for i in range(0,6):
del new[0]
len(new)
"""365 days in a year so only 1:364 have useful info
"""
for i in range(365, len(new)):
del new[365]
for i in range(0, len(new)):
new[i] = new[i].split('/')
new[i][2] = new[i][2].split('\t')
new[i].append(new[i][2][1])
new[i][2] = new[i][2][0]
for j in range (0,4):
new[i][j] = int(new[i][j])
new
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.
Jan = 0
Feb = 0
Mar = 0
Apr = 0
May = 0
Jun = 0
Jul = 0
Aug = 0
Sep = 0
Oct = 0
Nov = 0
Dec = 0
count = 0
"""Makes counter for each month and totals them. Count counter is for comparison of totals in each month
"""
for i in range(0, len(new)):
if new[i][0] == 1:
Jan = Jan + new[i][3]
elif new[i][0] == 2:
Feb = Feb + new[i][3]
elif new[i][0] == 3:
Mar = Mar + new[i][3]
elif new[i][0] == 4:
Apr = Apr + new[i][3]
elif new[i][0] == 5:
May = May + new[i][3]
elif new[i][0] == 6:
Jun = Jun + new[i][3]
elif new[i][0] == 7:
Jul = Jul + new[i][3]
elif new[i][0] == 8:
Aug = Aug + new[i][3]
elif new[i][0] == 9:
Sep = Sep + new[i][3]
elif new[i][0] == 10:
Oct = Oct + new[i][3]
elif new[i][0] == 11:
Nov = Nov + new[i][3]
elif new[i][0] == 12:
Dec = Dec + new[i][3]
else:
count = 0
"""Compare and replace
"""
month = "January"
count = Jan
if Feb > count:
count = Feb
month = "February"
else:
count = count
month = month
if Mar > count:
count = Mar
month = "March"
else:
count = count
month = month
if Apr > count:
count = Apr
month = "April"
else:
count = count
month = month
if May > count:
count = May
month = "May"
else:
count = count
month = month
if Jun > count:
count = Jun
month = "June"
else:
count = count
month = month
if Jul > count:
count = Jul
month = "July"
else:
count = count
month = month
if Aug > count:
count = Aug
month = "August"
else:
count = count
month = month
if Sep > count:
count = Sep
month = "September"
else:
count = count
month = month
if Oct > count:
count = Oct
month = "October"
else:
count = count
month = month
if Nov > count:
count = Nov
month = "November"
else:
count = count
month = month
if Dec > count:
count = Dec
month = "December"
else:
count = count
month = month
print("The month with most births is %s with %d births") % (month, count)
Sun = 0
Mon = 0
Tue = 0
Wed = 0
Thu = 0
Fri = 0
Sat = 0
more = 0
less = 0
"""Makes counter for each month and totals them. More and less counters are used to compare for most and least number of births
"""
for i in range(0, len(new)):
days = day(new[i][0], new[i][1], new[i][2])
if days == "Sunday":
Sun = Sun + new[i][3]
if days == "Monday":
Mon = Mon + new[i][3]
if days == "Tuesday":
Tue = Tue + new[i][3]
if days == "Wednesday":
Wed = Wed + new[i][3]
if days == "Thursday":
Thu = Thu + new[i][3]
if days == "Friday":
Fri = Fri + new[i][3]
if days == "Saturday":
Sat = Sat + new[i][3]
more = Sun
daymore = "Sunday"
"""Compare and replace
"""
if Mon > more:
more = Mon
daymore = "Monday"
else:
more = more
daymore = daymore
if Tue > more:
more = Tue
daymore = "Tuesday"
else:
more = more
daymore = daymore
if Wed > more:
more = Wed
daymore = "Wednesday"
else:
more = more
daymore = daymore
if Thu > more:
more = Thu
daymore = "Thursday"
else:
more = more
daymore = daymore
if Fri > more:
more = Fri
daymore = "Friday"
else:
more = more
daymore = daymore
if Sat > more:
more = Sat
daymore = "Saturday"
else:
more = more
daymore = daymore
less = Sun
dayless = "Sunday"
if Mon < less:
less = Mon
dayless = "Monday"
else:
less = less
dayless = dayless
if Tue < less:
less = Tue
dayless = "Tuesday"
else:
less = less
dayless = dayless
if Wed < less:
less = Wed
dayless = "Wednesday"
else:
less = less
dayless = dayless
if Thu < less:
less = Thu
dayless = "Thursday"
else:
less = less
dayless = dayless
if Fri < less:
less = Fri
dayless = "Friday"
else:
less = less
dayless = dayless
if Sat < less:
less = Sat
dayless = "Saturday"
else:
less = less
dayless = dayless
print("The week of day with most births is %s with %s births and the week of day with least births is %s with %s births") % (daymore, more, dayless, less)
When I look at it, I can't seem to draw any conclusions but the number of births for each day of the month and each week might be pretty close to each other.
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.
I think an effective way to present the information in the previous question is to categorize the number of births in different categories perhaps by month and day and present it all so that there can be a sense of order in them.