Project Euler 19

http://projecteuler.net/index.php?section=problems&id=19


これはライブラリを使う問題ではなく、与えられた1900年1月1日の曜日を元に計算するものです。yieldを使うと記述しやすいでしょう。

let is_leep_year y = y % 4 = 0 && (y % 400 = 0 || y % 100 <> 0)
let days = [| 0; 31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31; |]
let weekday_of_1st = seq {
    let w = ref 1
    for y in [1900..2000] do
        for m in [1..12] do
            yield ((y, m), !w)
            let d = if m = 2 then 
                        if is_leep_year y then 29 else 28
                      else days.[m]
            w := (!w + d) % 7
}

printfn "%d" (Seq.length
                    (Seq.filter (fun x -> snd x = 0 && fst (fst x) > 1900)
                    weekday_of_1st))