Project Euler — Problem 19 Solution


You are giv­en the fol­low­ing infor­ma­tion, but you may pre­fer to do some research for your­self.

  • 1 Jan 1900 was a Mon­day.
  • Thir­ty days has Sep­tem­ber, April, June and Novem­ber. All the rest have thir­ty-one, sav­ing Feb­ru­ary alone, which has twen­ty-eight, rain or shine. And on leap years, twen­ty-nine.
  • A leap year occurs on any year even­ly divis­i­ble by 4, but not on a cen­tu­ry unless it is divis­i­ble by 400.

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


open System

let ans =
    |> List.collect (fun y -> [1..12] |> (fun m -> new DateTime(y, m, 1)))
    |> List.filter (fun d -> d.DayOfWeek = DayOfWeek.Sunday)
    |> List.length

The solu­tion here is sim­ple, for the int list [1901..2000] gen­er­ates the Carte­sian prod­uct with the int list [1..12] to get a Date­Time object rep­re­sent­ing the first day in each month from 1901 to 2000, e.g.


From this point, all that’s left is to fil­ter the list of Date­Time val­ues to find the ones which rep­re­sent a Sun­day and count them.