1. 9
  1.  

  2. 5

    A simpler and easier way to calculate the day of the week for any date is the First Sunday Doomsday algorithm: http://firstsundaydoomsday.blogspot.com/2009/12/quick-start-guide.html

    (There’s also a earn-by-example page: http://firstsundaydoomsday.blogspot.com/2011/01/learn-by-example.html)

    Using Sundays rather than doomsdays simplifies the arithmetic. Another improvement the above approach incorporates is the odd+11 rule for calculating the year code.

    Also, here’s an easier method I created to determine if a year is a leap year:

    If the last two digits of the year are 00, throw them away and keep only the first two digits. Otherwise, keep only the last two digits. The year is a leap year if what you kept is divisible by 4.

    Examples:

    • The year 1705 does not end in 00, so throw away 17 and keep 05. 05 is not divisible by 4, so 1705 is not a leap year.

    • The year 2000 ends in 00, so throw away 00 and keep 20. 20 is divisible by 4, so 2000 is a leap year.

    The standard method [2] is more complicated:

    Years that are divisible by four are leap years, with the exception of years that are divisible by 100, which are not leap years, and with the final exception of years divisible by 400, which are.

    [1] https://twitter.com/jordancurve/status/1012203999716618240

    [2] https://www.theguardian.com/science/2011/feb/28/leap-year-alex-bellos

    1. 1

      Thanks a lot! I have been using the standard doomsday method for years and I find it very easy to use. However, I can also see the simplicity of the First Sunday Doomsday method, so I’ma give it a try. Thanks for the links!

      1. 1

        PS: The Guardian article about leap years says:

        The following rule, devised recently by British maths brainiac Adam P Goucher, is much cleverer: years divisible by 128 are not a leap year, otherwise years divisible by four are a leap year.

        I cannot easily divide by 128, but: 1900/128 = 14.84375 and 1900/4 = 475, so according to the rule above year 1900 should be a leap year. What am I missing?

        1. 2

          I think their check for divisibility by 128 is meant to short circuit if true, i.e.:

          is_leap_year = (if year % 128 == 0 then false else year % 4 == 0 end)

          1. 2

            I still had troubles understanding this because my premise all along was that it is a leap year test that is plugin-compatible with our 4/100/400 rule. Reading https://www.inverse.com/article/12152-how-to-make-a-better-leap-year-with-math I now see that this premise is false. They are talking about inventing an entirely new leap year system, not about easily checking which years in our current system that are leap years.

            1. 2

              PS: I like the following quote from the article:

              Admittedly, calculating what year makes for every 128 years isn’t the easiest, so Parker takes it one step further, suggesting we just write all our years in binary. If the last seven digits of a binary year are zeros, then we skip the leap year. This year is 11111100000 in binary, so we’re good.

              :)