Problem #19

Another Project Euler prob­lem has been solved! This one almost made me feel guilty as I put the high level libraries of Python to use.

How many Sun­days fell on the first of the month dur­ing the twen­ti­eth cen­tury (1 Jan 1901 to 31 Dec 2000)?

My code was short and sweet:

from date­time import date­time

d = date­time(1901,1,1)

total_sundays = 0

for yr in range(1901,2001):
    d = d.replace(year=yr)
    for m in range(1,13):
        d = d.replace(month=m)
        if(d.week­day() == 6):
            total_sundays += 1

print total_sundays

Sur­pris­ingly, the code was also very fast:

[email protected]:~$ time python
[[answer snip]]
real 0m0.086s
user 0m0.012s
sys 0m0.000s
[email protected]:~$

Leave a Reply