Zeller Congruence (The Program)

Now that we know that the algorithm works we can know build a solution

We will start by adding code to perform the calculation for this we will need to import the Python math module so that we can use the built in floor function. We will also have to take into account the months January and February. The code is as follows:

import math

def zeller(day, month, year):
    if month < 3:
        month = month + 12
        year = year -1
    calc = (day
            + math.floor((13*(month+1)/5))
            + year
            + math.floor(year/4)
            - math.floor(year/100)
            + math.floor(year/400)) % 7
    return calc

Running this function on the dates we calculated above we get:

>>> zeller(24,5,2020)
1
>>> zeller(1,1,2000)
0

These are exactly the results we got earlier so we can be sure that our code is correct. We can now get the function to return a day of the week by adding a list of days of the week and mapping the output to an item in the list, the revised function is:

import math

def zeller(day, month, year):
    days = ['Saturday', 'Sunday', 'Monday', 'Tuesday',
              'Wednesday', 'Thursday', 'Friday']
    if month < 3:
        month = month + 12
        year = year -1
    calc = (day
            + math.floor((13*(month+1)/5))
            + year
            + math.floor(year/4)
            - math.floor(year/100)
            + math.floor(year/400)) % 7
    return days[calc]

Running this code gives:

>>> zeller(24,5,2020)
'Sunday'
>>> zeller(1,1,2000)
'Saturday'

The code works.