Hey guys. I decided to do another project euler problem, and got this problem:

You are given the following information, but you may prefer to do some research for yourself.

    * 1 Jan 1900 was a Monday.
    * Thirty days has September,
      April, June and November.
      All the rest have thirty-one,
      Saving February alone,
      Which has twenty-eight, rain or shine.
      And on leap years, twenty-nine.
    * A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

After I come up with an algorithm, I normally check it against other one's on the web. Mine oddly seems faster and simpler than all the one's I've seen (never happend befor). I just want to make sure it makes sence, and I'm not missing something. Here is the algorithm:

year = 1901
day = 1      //tuesday
sum = 0

while year is less than 2001
  day += 31
  sum += day/7  //add the number of sundays in this january to sum
  day %= 7      //this is the day that this january end on
  if year is divisable by 4 and year is not divisible by 100  //leap year
    day += 335
  else if year is divisable by 4 and divisable by 100         //leap year
    day += 335
  else                                                        //not leap year
    day += 334
  day %= 7    //day now equals the first day fo january

Is there something I'm missing?
The other algorithms I've seen seem to use the fact that there are 1200 months in a century and requre a counter for day, week, month, year and sum.

8 Years
Discussion Span
Last Post by Hiroshe

This: if year is divisable by 4 and year is not divisible by 100 //leap year is not complete. A leap year is a year that's divisable by 4 and not by 100 unless it can be divided by 400.

Other then that, it looks fine.


Your right. I've finally got to implement it (14 hour drive) and it has problems still. I'll check over it again when I'm back home. I'm going to mark it as solved, since atleast one person thinks my algorithm is almost fine.
Thanks :)


Hehe... Yup I missed something. The question asked how many Sundays fall on the first of each month. Not how many Sundays fall on the first month. I knew there was something up :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.