Project Euler Problem 19

Counting Sundays.

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.

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

Link to original description
Source code examples on Github

Erlang version

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname p19
% vim:syn=erlang

-mode(compile).

main(_) ->
    io:format("Answer: ~p ~n", [length([ 1 || Y <- lists:seq(1901, 2000),  
                                              M <- lists:seq(1, 12), 
                                              calendar:day_of_the_week(Y,M,1) =:= 7 ])]).

Python version

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/python

import datetime

sundays = 0
for year in xrange(1901, 2001):
    for month in xrange(1, 13):
        d = datetime.date(year, month, 1)
        if d.weekday() == 6:
            sundays = sundays + 1

print sundays