Skip to content

Instantly share code, notes, and snippets.

@kohyama
Last active December 10, 2015 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohyama/4472323 to your computer and use it in GitHub Desktop.
Save kohyama/4472323 to your computer and use it in GitHub Desktop.
Project Euler Problem 19
;; 閏年かどうか
(defn leap? [year]
(and (zero? (mod year 4))
(or (pos? (mod year 100))
(zero? (mod year 400)))))
;; 西暦で与えられた年の各月の日数のリスト
(defn numbers-of-days [year]
(if (leap? year)
;;Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
'(31 29 31 30 31 30 31 31 30 31 30 31)
'(31 28 31 30 31 30 31 31 30 31 30 31)))
(defn pep019 []
(->>
;; 2001年1月から過去に向かって1900年1月までの
;; 最初の日の曜日 (0:日曜日, 1:月曜日, ...) のリスト
(reduce #(cons (mod (+ %2 (first %)) 7) %)
'(1) ; 1900年1月1日は月曜日
(apply concat (map numbers-of-days (range 1900 2001))))
next ; 2001年1月は除く
(take 1200) ; 2000年12月から過去に向かう順に対象1200月を得る
(filter (partial = 0)) ; 最初の日が日曜日になるのは
count)) ; 何回あるか
@kohyama
Copy link
Author

kohyama commented Jan 7, 2013

Project Euler Problem 19
「1900年1月1日は月曜日である。
9月、4月、6月、11月は30日まであり、2月を除く他の月は31日まである。
2月は28日まであるが、うるう年のときは29日である。
うるう年は西暦が4で割り切れる年に起こる。しかし、西暦が400で割り切れず100で割り切れる年はうるう年でない。
20世紀(1901年1月1日から2000年12月31日)で月の初めが日曜日になるのは何回あるか。」
ということです.

いろいろハードコードでやっつけですが, ライブラリを使わず, 与えられた情報から手動で計算した例です.

参照: #mitori_clj Project Euler Problem 19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment