home > Project Euler >

ForNext

Shut the fuck up and write some code

Problem 19

20世紀中に月の初めが日曜日になるのは何回あるか?

次の情報が与えられている.

- 1900年1月1日は月曜日である.
- 9月, 4月, 6月, 11月は30日まであり, 2月を除く他の月は31日まである.
- 2月は28日まであるが, うるう年のときは29日である.
- うるう年は西暦が4で割り切れる年に起こる. しかし, 西暦が400で割り切れず100で割り切れる年はうるう年でない.

20世紀(1901年1月1日から2000年12月31日)中に月の初めが日曜日になるのは何回あるか?

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)?

考え方

曜日の計算に、「ツェラーの公式」というものがある。
こちらのページを参考にさせていただいた。→ http://www.kkaneko.com/adp/ad/date.html

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

更新日 : 2013.02.01
scala> def weekday(year:Int, month:Int, d:Int) = {
     |     var y = year
     |     var m = month
     |     if (m == 1 || m == 2) {
     |         y -= 1
     |         m += 12
     |     }
     |     (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7
     | }
weekday: (year: Int, month: Int, d: Int)Int
scala> weekday(2013, 2, 1)
res0: Int = 5
scala> var i = 0
i: Int = 0

scala> for (y <- 1901 to 2000; m <- 1 to 12) {
     |     if (weekday(y, m, 1) == 0) i += 1
     | }

scala> i
res2: Int = 171

F#

更新日 : 2013.02.01
> let weekday year month d =
-     let mutable y = year
-     let mutable m = month
-
-     if (m = 1 || m = 2) then
-         y <- y - 1
-         m <- m + 12
-     (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7
- ;;

val weekday : int -> int -> int -> int
> weekday 2013 2 1
- ;;
val it : int = 5
> let mutable i = 0
- for y in [1901..2000] do
-     for m in [1..12] do
-         if (weekday y m 1) = 0 then i <- i + 1
- ;;

val mutable i : int = 171

C

C++

C++Builder

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system