Created
April 22, 2012 13:26
-
-
Save ponkore/2464119 to your computer and use it in GitHub Desktop.
Project Euler Problem 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Project Euler Problem 2 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%202 | |
;;; | |
;;; フィボナッチ数列の項は前の2つの項の和である。 最初の2項を 1, 2 とすれば、最初の10項は以下の通りである。 | |
;;; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | |
;;; 数列の項の値が400万を超えない範囲で、偶数値の項の総和を求めよ。 | |
;;; フィボナッチ数列を、シーケンスとして求める。 | |
(defn fib1 [[a b]] [b (+' a b)]) | |
(defn fib [] (map second (iterate fib1 [1 1]))) | |
;;; ex. (take 10 (fib)) => (1 2 3 5 8 13 21 34 55 89) | |
;;; 指定された上限を超えない範囲で偶数のものを抽出する。 | |
(defn fib-even-limited [max] (filter #(even? %) (take-while #(< % max) (fib)))) | |
;;; 400万を上限として結果を合計。 | |
(apply + (fib-even-limited (* 400 10000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment