954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How many sundays fall on january in the first century

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
  year++

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.
Thanks.

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

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 100unless it can be divided by 400.

Other then that, it looks fine.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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 :)

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

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 :)

Hiroshe
Posting Whiz in Training
256 posts since Jun 2008
Reputation Points: 431
Solved Threads: 17
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You