Problem #19 Difficulty: 5%

Counting Sundays

Solution Language: Java

The problem

Count the months between January 1901 and December 2000 whose first day fell on a Sunday, using the Gregorian leap-year rules.

My solution

Neither version uses a date library. Both start from the fact that 1 January 1901 was a Tuesday (the problem gives 1 January 1900 as a Monday, and 1900 has 365 days), with days of the week numbered 0 = Sunday … 6 = Saturday.

Java (Euler19.java) simulates the calendar one day at a time. It tracks date, month, year and day, counts whenever date == 1 && day == 0, and rolls the month over on day 29 of a non-leap February, day 30 of February, day 31 of the 30-day months, or day 32 of any month. leap() implements the full rule (divisible by 400, else not by 100, else by 4). That’s about 36,500 iterations.

Python (Euler19.py, Python 2, added in 2016) steps a month at a time instead. It checks whether the current first-of-month is a Sunday, then advances the weekday by that month’s length mod 7:

for y in range(1901, 2001):
  for m in range(12):
    if day == 0:
      count += 1
    day = (day + month[m]) % 7
    if m == 1 and is_leap(y):
      day = (day + month[m]) % 7

That’s 1,200 iterations instead of 36,500.