Skip to content

Instantly share code, notes, and snippets.

@ponkore
Created April 22, 2012 13:26
Show Gist options
  • Save ponkore/2464119 to your computer and use it in GitHub Desktop.
Save ponkore/2464119 to your computer and use it in GitHub Desktop.
Project Euler Problem 2
;;; 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