home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / sci-math-faq / dayWeek < prev    next >
Text File  |  1995-11-18  |  7KB  |  186 lines

  1. Newsgroups: sci.math,sci.answers,news.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!torn!watserv3.uwaterloo.ca!undergrad.math.uwaterloo.ca!neumann.uwaterloo.ca!alopez-o
  3. From: alopez-o@neumann.uwaterloo.ca (Alex Lopez-Ortiz)
  4. Subject: sci.math FAQ: Day of Week 
  5. Summary: Part 34 of many, New version,
  6. Originator: alopez-o@neumann.uwaterloo.ca
  7. Message-ID: <DI76nB.1p8@undergrad.math.uwaterloo.ca>
  8. Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
  9. Approved: news-answers-request@MIT.Edu
  10. Date: Fri, 17 Nov 1995 17:16:23 GMT
  11. Expires: Fri, 8 Dec 1995 09:55:55 GMT
  12. Reply-To: alopez-o@neumann.uwaterloo.ca
  13. Nntp-Posting-Host: neumann.uwaterloo.ca
  14. Organization: University of Waterloo
  15. Followup-To: sci.math
  16. Lines: 167
  17. Xref: senator-bedfellow.mit.edu sci.math:124408 sci.answers:3442 news.answers:57843
  18.  
  19.  
  20. Archive-Name: sci-math-faq/dayWeek
  21. Last-modified: December 8, 1994
  22. Version: 6.2
  23.  
  24.  
  25. How to determine the day of the week, given the month, day and year
  26.  
  27.  
  28.  
  29.    First a brief explanation: In the Gregorian Calendar, over a period of
  30.    four hundred years, there are 97 leap years and 303 normal years. Each
  31.    normal year, the day of January 1 advances by one; for each leap year
  32.    it advances by two.
  33.  
  34.    303 + 97 + 97 = 497 = 7 * 71
  35.  
  36.    As a result, January 1 year N occurs on the same day of the week as
  37.    January 1 year N + 400 . Because the leap year pattern also recurs
  38.    with a four hundred year cycle, a simple table of four hundred
  39.    elements, and single modulus, suffices to determine the day of the
  40.    week (in the Gregorian Calendar), and does it much faster than all the
  41.    other algorithms proposed. Also, each element takes (in principle)
  42.    only three bits; the entire table thus takes only 1200 bits, or 300
  43.    bytes; on many computers this will be less than the instructions to do
  44.    all the complicated calculations proposed for the other algorithms.
  45.  
  46.    Incidental note: Because 7 does not divide 400, January 1 occurs more
  47.    frequently on some days than others! Trick your friends! In a cycle of
  48.    400 years, January 1 and March 1 occur on the following days with the
  49.    following frequencies:
  50.  
  51.  
  52.            Sun      Mon     Tue     Wed     Thu     Fri     Sat
  53.     Jan 1: 58       56      58      57      57      58      56
  54.     Mar 1: 58       56      58      56      58      57      57
  55.  
  56.  
  57.  
  58.    Of interest is that (contrary to most initial guesses) the occurrence
  59.    is not maximally flat.
  60.  
  61.    The Gregorian calendar was introduced in 1582 in parts of Europe; it
  62.    was adopted in 1752 in Great Britain and its colonies, and on various
  63.    dates in other countries. It replaced the Julian Calendar which has a
  64.    four-year cycle of leap years; after four years January 1 has advanced
  65.    by five days. Since 5 is relatively prime to 7, a table of 4 * 7 = 28
  66.    elements is necessary for the Julian Calendar.
  67.  
  68.    There is still a 3 day over 10,000 years error which the Gregorian
  69.    calendar does not take into account. At some time such a correction
  70.    will have to be done but your software will probably not last that
  71.    long!
  72.  
  73.    Here is a standard method suitable for mental computation:
  74.  
  75.     1. Take the last two digits of the year.
  76.     2. Divide by 4, discarding any fraction.
  77.     3. Add the day of the month.
  78.     4. Add the month's key value: JFM AMJ JAS OND 144 025 036 146
  79.     5. Subtract 1 for January or February of a leap year.
  80.     6. For a Gregorian date, add 0 for 1900's, 6 for 2000's, 4 for
  81.        1700's, 2 for 1800's; for other years, add or subtract multiples
  82.        of 400.
  83.     7. For a Julian date, add 1 for 1700's, and 1 for every additional
  84.        century you go back.
  85.     8. Add the last two digits of the year.
  86.     9. Divide by 7 and take the remainder.
  87.  
  88.  
  89.  
  90.    Now 1 is Sunday, the first day of the week, 2 is Monday, and so on.
  91.  
  92.    The following formula, which is for the Gregorian calendar only, may
  93.    be more convenient for computer programming. Note that in some
  94.    programming languages the remainder operation can yield a negative
  95.    result if given a negative operand, so mod 7 may not translate to a
  96.    simple remainder. W = (k + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) +
  97.    floor(C/4)) mod 7 where floor() denotes the integer floor function,
  98.    k is day (1 to 31)
  99.    m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) Treat
  100.    Jan &Feb as months of the preceding year
  101.    C is century (1987 has C = 19)
  102.    Y is year (1987 has Y = 87 except Y = 86 for Jan &Feb)
  103.    W is week day (0 = Sunday, ..., 6 = Saturday)
  104.    Here the century and 400 year corrections are built into the formula.
  105.    The floor(2.6m - 0.2) term relates to the repetitive pattern that the
  106.    30-day months show when March is taken as the first month.
  107.  
  108.    The following short C program works for a restricted range
  109.  
  110.  
  111. dow(m,d,y){y-=m<3;return(y+y/4-y/100+y/400+"-bed=pen+mad."[m]+d)%7;}
  112.  
  113.  
  114.  
  115.    The program appeared was posted by sakamoto@sm.sony.co.jp (Tomohiko
  116.    Sakamoto) on comp.lang.c on March 10th, 1993.
  117.  
  118.    A good mnemonic rule to help on the computation of the day of the week
  119.    is as follows. In any given year the following days come on the same
  120.    day of the week:
  121.  
  122.  
  123. 4/4
  124. 6/6
  125. 8/8
  126. 10/10
  127. 12/12
  128.  
  129.  
  130.  
  131.    to remember the next four, remember that I work from 9-5 at a 7-11 so
  132.  
  133.  
  134. 9/5
  135. 5/9
  136. 7/11
  137. 11/7
  138.  
  139.  
  140.  
  141.    and the last day of Feb.
  142.  
  143.    "In 1995 they come on Tuesday. Every year this advances one other than
  144.    leap-years which advance 2. Therefore for 1996 the day will be
  145.    Thursday, and for 1997 it will be Friday. Therefore ordinarily every 4
  146.    years it advances 5 days. There is a minor correction for the century
  147.    since the century is a leap year iff the century is divisible by 4.
  148.    Therefore 2000 is a leap year, but 1900, 1800, and 1700 were not."
  149.  
  150.    Even ignoring the pattern over for a period of years this is still
  151.    useful since you can generally figure out what day of the week a given
  152.    date is on faster than someone else can look it up with a calender if
  153.    the calender is not right there. (A useful skill that.)
  154.  
  155.  
  156.  
  157.    References
  158.  
  159.    Winning Ways for your mathematical plays. Guy Conway and Elwyn
  160.    Berlekamp London ; Toronto : Academic Press, 1982.
  161.  
  162.  
  163.  
  164.    Mathematical Carnival. Martin Gardner. New York : Knopf, c1975.
  165.  
  166.  
  167.  
  168.    Elementary Number Theory and its applications. Kenneth Rosen. Reading,
  169.    Mass. ; Don Mills, Ont. : Addison-Wesley Pub. Co., c1993. p. 156.
  170.  
  171.  
  172.  
  173.    Michael Keith and Tom Craver. The Ultimate Perpetual Calendar? Journal
  174.    of Recreational Mathematics, 22:4, pp. 280-282, 19
  175.  
  176.  
  177.  
  178.  
  179.      _________________________________________________________________
  180.  
  181.  
  182.  
  183.     alopez-o@barrow.uwaterloo.ca
  184.     Tue Apr 04 17:26:57 EDT 1995
  185.  
  186.