Implementing the Doomsday Rule

This is a discussion on Implementing the Doomsday Rule within the C++ Programming forums, part of the General Programming Boards category; For those of you not familiar with this rule: http://rudy.ca/doomsday.html I'm trying to implement this algorithm into a program, and ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Implementing the Doomsday Rule

    For those of you not familiar with this rule: http://rudy.ca/doomsday.html

    I'm trying to implement this algorithm into a program, and up until now it has been going fine. I've encountered a problem now though:

    I'm trying to find the anchor day of any given century (The program will only accept input from 0000 to 10000)

    The anchor days can only be Sundays, Tuesdays, Wednesdays and Fridays, so i'm disregarding the other days.

    The algorithm comes up with a number between 1 and 4, 1 is sunday, 2 is tuesday, 3 is wednesday and 4 is obviously friday, or well, atleast that is what it's supposed to do. The loop i'm using starts out at 0, and then increments by 100 and adds 1 to i each time it loops. Then when it reaches the century that was inputted it does i (mod 4).

    This is the code i have so far:
    Code:
    /* Find the anchor day of the century */
                    for(n = 0; n != century; i++)
    		{
    			n += 100;
    		}
    		i %= 4;
    The output i get is wrong, 1900 evaluates to 3 (Which is correct - wednesday), but 1800 is 2 (Which should have been 4) and 2000 becomes 4 (Which in turn should have been 2). It seems like i got it upside down, but how can i fix it then? Has anyone got experience with the doomsday rule, or maybe some of you math experts can come up with a solution for me

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Neo1 View Post
    I'm trying to implement this algorithm into a program
    First of all you seem to be trying to do the calculations using a formula that assumes that the Gregorian calendar has been in effect since the year zero. Beside the fact that your assignment of weekdays corresponding to 0, 1, 2, 3 gives the wrong answers, there are two other fundamental errors:

    1. There was no year zero.

    2. The Gregorian Reformation occurred on September 3, 1752, at which time ten days were "sklpped". This is mentioned on the web site that you linked us to.

    Note that the "real" February, 1700, which was on the Julian calendar, looks like the following, and "Doomsday" for 1700 is not correctly calculated from the algorithm on the site that you referenced.

    Code:
        February 1700
    Su Mo Tu We Th Fr Sa
                 1  2  3
     4  5  6  7  8  9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29
    Date calculations before September 14, 1752, should take the "missing" 10 days into account.

    It is important to know the difference if you use other places as a reference to check the answers that you finally end up with. And, of course, you have to check your answers, somehow, against some other reference, right? (Never get all of your information from one source, I say.)


    Forgetting that for a moment, look at the days for the "Doomsday" in the century years starting with 1800:


    1800: Friday
    1900: Wednesday
    2000: Tuesday
    2100: Sunday
    2200: Friday
    2300: Wednesday
    2400: Tuesday
    2500: Sunday


    I guess you could extend this backwards to the year zero if you wanted, but the actual dates would not be correct on "real" calendars for century years before 1800.

    Anyhow, if you wanted to make some kind of mathematical relation between the century and the day of the week, you could write the following, just using the century = (year / 100)

    18: Friday
    19: Wednesday
    20: Tuesday
    21: Sunday
    22: Friday
    23: Wednesday
    24: Tuesday
    25: Sunday

    Since there is a periodicity of four, we notice that a division of the century modulo 4 gives all the information that is needed:

    18%4 = 2 ---Friday
    19%4 = 3 ---Wednesday
    20%4 = 0 ---Tuesday
    21%4 = 1 ---Sunday
    22%4 = 2 ---Friday
    23%4 = 3 ---Wednesday
    24%4 = 0 ---Tuedsay
    25%4 = 1 ---Sunday

    Putting these together, you could create a program based on the following:

    Instead of using a loop to count the number of centuries from the non-existent year zero (which would have had a doomsday of Tuesday if there were a year zero and if the Gregorian calendar had been in effect at that time), Just take the century modulo 4 and use the following relationship:

    (century % 4) == 0 Implies that doomsday for year 00 of that century is Tuesday
    (century % 4) == 1 implies that doomsday for year 00 of that century is Sunday
    (century % 4) == 2 implies that doomsday for year 00 of that century is Friday
    (century % 4) == 3 implies that doomsday for year 00 of that centuryr is Wednesday


    D
    Last edited by Dave Evans; 08-17-2007 at 12:09 AM.

  3. #3
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    2. The Gregorian Reformation occurred on September 3, 1752, at which time ten days were "sklpped". This is mentioned on the web site that you linked us to.
    That's not quite correct, it's true that Great Britain and its colonies switched from the Julian calender to the Gregorian one in 1752, but pretty much the rest of europe switched in 1582.

    Go to Wikipedia for a complete explanation of a working algorithm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Misra Rule 11.2
    By hammer1234 in forum C Programming
    Replies: 1
    Last Post: 04-06-2006, 08:28 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Rule of 72
    By sonict in forum C++ Programming
    Replies: 12
    Last Post: 01-23-2003, 08:31 PM
  4. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 07:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21