The author is a Forbes contributor. The opinions expressed are those of the writer.

Loading ...

Loading ...

This story appears in the {{article.article.magazine.pretty_date}} issue of {{article.article.magazine.pubName}}. Subscribe

(Photo by Win McNamee/Getty Images)

One of my fellow contributors here recently wrote a post about leap years and why we have them. This reminded me of modular arithmetic and the formula for calculating the day of the week for any date in history.

We use modular arithmetic all the time. It's useful for situations where you want to start over, time being the most obvious. Say it's 9 o'clock. What time will it be 5 hours from now? Unless you're in the military you'd say 2 o'clock, right? That's modular arithmetic, the modulus being 12. To add hours, we simply add them together: 9+5=14, and then subtract 12 to get 2. Months work the same: 12 months means that 5 months after September is the second month of the next year (February). Ditto for minutes (modulo 60) and hours (arithmetic modulo 60).

In general, given a positive integer n, we have the relation of congruence modulo n: an integer a is equivalent to its remainder when divided by n. So there are only n distinct classes of integers modulo n, namely the n remainders 0,1,2,...(n-1). We can add these together just like we do hours or months, subtracting off n if the sum of two remainders exceeds n.

Arithmetic modulo 2 is all about odd vs. even. The even integers correspond to remainder 0 when dividing by 2, and the odds have remainder 1 when dividing by 2. The two remainders may then be added, keeping in mind this relationship: 0+0=0, 0+1=1, 1+0=1, 1+1=0. That last one happens because 1+1=2, and so we subtract 2 to get the right remainder class (0). These sums are simply a formal algebraic way of saying that even + even = even, odd + even = odd, and odd + odd = even. Computers use mod 2 addition all the time since they store numbers in binary.

So what about days of the week? That is arithmetic modulo 7 of course and there is a formula that will give you the day of the week for any date in history. It's called Zeller's formula and here it is:

The various terms are defined as follows: d is the day of the week, where 0 = Sunday, 1 = Monday ... 6 = Saturday; k is the day of the month; D is the last two digits of the year; C is the century, obtained as the first two digits of the year. The brackets [ • ] correspond to the greatest integer function, which is the largest integer less than or equal to the number inside. So, for example [π]=3. The number m is the month, but there's a catch: we start counting with March = 1 so that January and February are the 11th and 12th months of the previous year.

Let's do some examples. I'll pick a date we know: December 7, 1941 was a Sunday. The corresponding numbers are: k = 7, D = 41, C = 19, m = 10. Then we get

So that one works. How about January 13, 2045?

I chose that date at random, and it turned out to be a Friday the 13th. Odd. Note the shift in years since January of 2045 is the 11th month of 2044 under this scheme.

Note that this formula only works for the Gregorian calendar, instituted by Pope Gregory VIII in 1582. Before that the Julian calendar was in effect. The protestant world resisted adoption of a Catholic innovation; indeed, the British empire (including the U.S.) didn't switch to the Gregorian calendar until 1752. The difference involves how leap years get tabulated; the Julian calendar made every fourth year a leap year, leading to an overcompensation for the extra few hours more than 365 days a year actually is. The Gregorian calendar fixes this by making years divisible by 100 non-leap years, unless the year is divisible by 400 in which case it is a leap year (so, 2000 was a leap year, but 2100 will not be). There is a Julian calendar formula that is similar.

By now you've calculated the day of the week on which you were born (mine is a Tuesday) and maybe a few others. This formula isn't too difficult to memorize if you want to bore amaze your friends at parties. Enjoy!