Fun with Mathematics!

[1 + 2 = 3]

Finding Day of Week

Have you ever imagined how complex a formula for finding the day of week would be? Well, the following simple formula using integer and mod functions was developed by Karl F. Gauss. Believe me! It is accurate for any given date!

First, let me introduce some variables:

d
This is the day. For example, d is 22 if the date is Oct 22, 1994.

m
This is the month, but slightly displaced. March is 1, April is 2, May is 3, ...., December is 10, January is 11 and February is 12. For example, m is 8 if the date is Oct 22, 1994.

Y
This is the year, but slightly displaced too. If the month is not January or February, Y would be equal to the year, else Y would be one less than the year. For example, Y is 1994 if the date is Oct 22, 1994; but Y would be 1993 if the date is Jan 8, 1994.

c
This is the first two digits of Y. For example, c is 19 if the date is Oct 22, 1994.

y
This is the last two digits of Y. For example, y is 93 if the date is Jan 8, 1994.

Let A = int(2.6m - 0.2) + d + y + int(y/4) + int(c/4) - 2c and v = A mod 7 where int(x) is the integer part of x, and x mod y is the value of the remainder when x is divided by y.

The value of v corresponds to the day of week in the following way: 0 is Sunday, 1 is Monday, 2 is Tuesday, ...., and 6 is Saturday.

As an example, let us calculate the day of the week for Jan 8, 1995:
Here, d = 8, m = 11, Y = 1994, c = 19 and y = 94.
A = int(28.4) + 8 + 94 + int(94/4) + int(19/4) - 38 = 119
v = 119 mod 7 = 0
Jan 8, 1995 is a Sunday! Check your calendar and see if it's correct!

Important Note:
Due to the perculiarity of the mod function for negative numbers in many programming languages, the above formula may seem wrong when programmed. In most programming languages, (-x) mod y is equivalent to x mod y, which is not the correct mathematical definition of mod, hence the problem.

To get around this problem, you can define your own mod function to take care of negative numbers. When computing x mod y, after computing the mod, see if it is zero. If it is, leave it. If not, then subtract the result from y. For example, when computing -5 mod 4, the C language will give you 1. Subtracting from 4 gives 4 - 1 = 3.


changkai@alum.mit.edu
since 1 Jan 98