Created
December 11, 2010 05:36
-
-
Save will/737183 to your computer and use it in GitHub Desktop.
euler38.clj
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
| (defn split [n] | |
| (cond | |
| (< n 10) [n] | |
| :else (conj | |
| (split (quot n 10)) | |
| (rem n 10)))) | |
| (defn join [numbers] | |
| (reduce | |
| #(+ (* 10 %1) %2) | |
| (reduce into (map split numbers)))) | |
| (defn check [pan n] | |
| (loop [pan (split pan), i 1] | |
| (let [n (* n i), nsp (split n), size (count nsp)] | |
| (cond | |
| (= nsp pan) true | |
| (= nsp (take size pan)) (recur (drop size pan) (inc i)) | |
| :else false)))) | |
| (defn from-concat? [pan] | |
| (some | |
| (partial check pan) | |
| (for [i (range 1 6)] (->> pan split (take i) join)))) | |
| (use 'clojure.contrib.combinatorics) | |
| (def pans (permutations [9 8 7 6 5 4 3 2 1])) | |
| (prn (join (first (filter #(from-concat? (join %)) pans)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment