大きな数の下10ケタを求める問題です。
2万人以上の人が解いている問題なので簡単です。
まずは、下10桁までの計算をする関数を作ります。計算して10の10乗の剰余を
取るものです。できたら、それを使って、reduceで元の数に2を必要なだけ掛
けていきます。最後に1を足したら出来上がり。
やってみたら、意外と時間がかかったので、正直に計算していた2のところを
いくつかまとめてみることにした。4を掛けるのであれば半分、8を掛けるので
あれば4分の1...になります。
ということで、何個か試してみてそれ以上増やしてもあまり変わらなくなった
ところでやめ。
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
;; Problem 97 : 2013/06/04 | |
;; _1 "Elapsed time: 16178.19302 msecs" | |
;; _2 "Elapsed time: 2027.017307 msecs" | |
(require '[clojure.math.numeric-tower :as math]) | |
(defn calc-in-n-digits [op opr1 opr2 n] | |
(mod (op opr1 opr2) (math/expt 10 n))) | |
(defn calc-in-n-digits [n op & oprs] | |
(mod (apply op oprs) (math/expt 10 n))) | |
(defn pe87_1 [] | |
(inc (reduce #(calc-in-n-digits * %1 %2 10) 28433 (repeat 7830457 2)))) | |
(defn pe87_2 [] | |
(inc (reduce #(calc-in-n-digits * %1 %2 10) 28433 (concat (repeat 978807 256) | |
[2])))) | |
0 コメント:
コメントを投稿