Project Euler with Ruby on WSL [Problem 19]

やれやれ

まだ 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.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

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

私訳

日曜日を数えよ

以下の事実を与えるが、追加で調べてもよい - 1900/1/1 は月曜日 - 30日の月は 9, 4, 6, 11 月で 2 月は 28日 or 29日で残りの月は 31 日 - 閏年は西暦が 4 の倍数かつ 100の倍数でなく、しかし 400 の倍数の年

月の最初が日曜日の月は、20世紀の間にいくつあったか?

解答方針

各月一日の通算日を求める。

で、その日通算日を 7 で割った余りを求めていけばよい

問題で与えられているのは 1900/1/1 だが、数え始めるのは 1901/1/1 からなので、 これは先に出してしまったほうがよさそうだ。1900年は閏年ではないため、  365 = 350 + 15 \equiv 1 \pmod 7 日分曜日がずれる。つまり 1901 年は火曜日始まり。