Created
January 9, 2013 14:18
-
-
Save ponkore/4493455 to your computer and use it in GitHub Desktop.
Project Euler Problem 25
This file contains 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
(ns projecteuler.problem-25 | |
(:require [clojure.string :as str] | |
[clojure.math.numeric-tower :as math]) | |
(:use clojure.test)) | |
(defn fib-gen | |
"フィボナッチ数を generate する関数。初期値[1 1]から iterate する前提。" | |
[[a b]] [b (+' a b)]) | |
(def fib-seq | |
"フィボナッチ数とその項を返す。" | |
(->> (iterate fib-gen [1 1]) | |
(map first) | |
(map-indexed (fn [idx itm] [(inc idx) itm])))) | |
(defn find-min-fibs | |
"lower 以上で最小のフィボナッチ数とそのindexを返す。" | |
[lower] | |
(first (drop-while #(<= (second %) lower) fib-seq))) | |
(defn problem-25 | |
"Project Euler Problem 25" | |
([] (problem-25 (math/expt 10 999))) | |
([lower] (first (find-min-fibs lower)))) | |
;;(time (problem-25)) | |
;;=> "Elapsed time: 5.387 msecs" | |
;; test | |
(is (= (find-min-fibs 100) [12 144])) | |
(is (= (problem-25 100) 12)) |
いや、ほんのちょびっとしか翻訳していないんです...偉そうにリンク出してしまったのですが、なんだか申し訳ない(暇できたらまたやります!!)。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ypsilon-takai さん
fib-seq
の件、確かにその通りかもしれません。名前fib-seq
と中身が一致してないから余計に違和感を増している感じですね。plaster さんのコメントにあるfibs
がfib-seq
にふさわしいかも。plaster さん