Euler : Problem 4

Posted by TAKAIY On 2011年4月9日土曜日 0 コメント
回文数の問題

10秒考えて戦略決定。
数字を作ってから3けたで割りきれるか確認する。

6桁だけでいいだろうと思ったけど、5桁の処理も作った。

;;
;; Problem 4 : 2011/4/7
(* 999 999) ;998001
(* 100 100) ;10000

(use 'clojure.contrib.math)

(defn palindromic-5-digit-num []
(for [one (range 9 1 -1) two (range 9 0 -1) three (range 9 0 -1)]
(sort (+ (* 10000 one) (* 1000 two) (* 100 three) (* 10 two) one))))

(defn palindromic-6-digit-num []
(for [one (range 9 0 -1) two (range 9 -1 -1) three (range 9 -1 -1)]
(+ (* 100000 one) (* 10000 two) (* 1000 three)
(* 100 three) (* 10 two) one)))

(defn is-multiple-of-three-digits? [n]
(loop [tester (range 100 1000)]
(let [target (first tester)]
(cond (empty? tester) false
(and (zero? (rem n target))
(>= (/ n target) 100 )
(< (/ n target) 1000)) (list target (/ n target))
true (recur (rest tester))))))

(loop [digit-list (palindromic-6-digit-num)]
(let [ans (is-multiple-of-three-digits? (first digit-list))]
(if (seq? ans)
[(first digit-list) ans]
(recur (rest digit-list)))))

(loop [digit-list (palindromic-5-digit-num)]
(let [ans (is-multiple-of-three-digits? (first digit-list))]
(if (seq? ans)
[(first digit-list) ans]
(recur (rest digit-list)))))
;;

0 コメント:

コメントを投稿