Você está na página 1de 4

August 2005

A simple Gregorian calendar algorithm based upon single-digit numbers


Richard Hennacy Westerville, OH Ken Hennacy University of Maryland 3257 A.V. Williams Bldg. Computer Science Department College Park, MD 20742

Abstract We present a simple Gregorian calendar algorithm based upon single-digit numbers. It was designed so that a person could perform the calculations easily without the aid of pencil or paper. The algorithm is simpler and more powerful than other computational methods due to its explicit reliance upon patterns that exist for the days, months, and centuries. These patterns are very significantit is more useful to recall them than remembering the number of days in a given month. Introduction Calendar algorithms have been in use for a long time. Historically, most compact calendar algorithms have been derived to keep track of religious events while others involve lookup tables (known as perpetual calendars) that can be found in references such as the Farmers Almanac. Such tables in the modern era are based upon the scheme invented by a physician known as Aloysius Lilius. This scheme is named the Gregorian calendar after Pope Gregory XIII who decreed in 1582 that it should replace the Julian calendar. The rest of the world took a while to adopt this change, howeverfor example it wasnt until 1752 that the calendar was officially recognized by England. There are a few compact algorithms that can be used to compute calendar information without lookup tables. One such algorithm is Zellers algorithminvented in the late 1800s. Though it can compute the day of the week to associate with a given date, it is not convenient for other types of computations. In all, previously existing compact algorithms do not explicitly recognize many of the patterns that we see within the calendars from year to year. For example, every 28 years there is a repeating cycle in the alignment of the calendar months; however this cycle gets shifted due to the 400 year adjustment in the leap year. Many of us have been aware that such patterns exist, however perpetual calendar algorithms in use today do not represent these patterns in a convenient fashion that we can easily remember. In this paper, we present an algorithm that accomplishes this goal.

A Pattern-based Calendar Algorithm The following assignments for the Day, Month, and Century need to be remembered:
Sun Mon Tue Wed Thu Fri Sat 0 1 2 3 4 5 6 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 6 2 2 5 0 3 5 1 4 6 2 4 1700 5 1800 3 1900 1 2000 0 2100 5 2200 3 2300 1 2400 0

D=

M=

C=

Here, a base 7 representation for the days of the week is used. This is typically done with calendar algorithms since the week represents the most basic cyclical pattern associated with the calendar. Another cyclical pattern exists for the centuries, requiring the memorization of only 4 numbers in the sequence shown. The month assignments implicitly recognize alignments that occur between the months. For a given year, mcyy, a replacement number is computed as shown:
Y = ( C + yy + yy div 4 ) mod 7

Here, the notation yy is used to recognize the 2-digit nature of the variable. The single digit, Y is the result of modulo arithmetic. To simplify the computation, it is preferable to apply the modulo function to each number before addition is performed. For example, the year 1926 would have the following replacement associated with it:
Y = [ 1 + 5 (5 = 26 mod 7) + 6 (6 = 26 div 4) ] mod 7 = 5

Another fact that can be taken advantage of is the 28 year cycle. One can replace yy by yy mod 28 (which equals 0 for 0, 28, 56, 84) to speed up calculations further. For example, given the year 1989, yy mod 28 = 5:
Y = [ 1 + 5 + 1 ] mod 7 = 0

For computations associated with the present, it is easier to remember what this number is than to compute it each time. For the year 2005, Y = 6 or equivalently in modulo arithmetic, Y=-1. Most of the calendar computations are derived from the relationship that exists between the day of the month, dd , and the associated day of the week, D:
D = ( dd + M + Y ) mod 7

For example, given the date June 13, 1926, dd = 13, M = 3, and Y = 5:
D = ( 6 + 3 + 5 ) mod 7 = Sun

There is an exception to this formula that occurs for the months of January and February on a leap year. Whenever mcyy is a leap year, the number M associated with these two months should be one less, i.e. 5 for January and 1 for February. For example, given the date January 7, 2004, dd = 7, M = 5, and Y = 5:
D = ( 7 + 5 + 5 ) mod 7 = Wed

The reason these two months are singled out traces back to the Roman calendar in which February was the last (and shortest) month of the yearmaking it the natural candidate for adding a leap day. The day formula can be rearranged to determine which months have the same day assigned to a particular date dd:
M = [ D dd Y ] mod 7

For example: What months in 1789 have a Fri 13th?


M = [ 5 6 4 ] mod 7 = 2 Feb, Mar, Nov

Such a computation is not easily performed with other methods. To determine the date associated with a given day of the week in a month, the following formula can be used:
dd = (W1)*7 + [ D M Y ] mod 7

For example: What is the date for the 4th Tuesday in December, 1845?
dd = (41)*7 + [ 2 4 3 ] mod 7 = 23rd

To determine which year has a particular date, the following two formulas for Y must be used:
Y = [ D dd M ] mod 7 Y = ( C + yy + yy div 4 ) mod 7

For example: What years have April 3rd on a Tuesday?


Y = [ 2 3 5 ] mod 7 = 1

For the century 2000, we need to find a year yy such that yy + yy div 4 = 1. Upon inspection, these years are 2001, 2007, 2012, 2018, and so on. Finally, to compute the number of days in a given month, one can compare dates computed from adjacent months: How many days are there in the month of July? Ans: Assume that July has 30 days. Choose any year such as 2000.
D(July) = [ 30 + 5 + 0 ] = Sun D(Aug) = [ 1 + 1 + 0 ] = Tue

Due to the separation between days of the week for the two dates, one concludes that July actually must have 31 days. Additional computations can be performed based upon these types of manipulations of the basic formula. Calendars themselves can be generated for a particular year by repeatedly applying the formula to compute the 1st of every month and verifying the consistency of the day assignments at the end of the month as shown above.

Summary A simple Gregorian calendar algorithm has been presented that can be used to compute the answer to a variety of questions about calendars. This algorithm represents a compressed form of knowledge representation that streamlines computations while preserving patterns for immediate recollection. Based upon our analysis, we expect this to be the simplest possible algorithm that can be used to accomplish the variety of computations that have been presented.

Você também pode gostar