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.