Skip to content

Instantly share code, notes, and snippets.

@will
Created December 11, 2010 05:36
Show Gist options
  • Select an option

  • Save will/737183 to your computer and use it in GitHub Desktop.

Select an option

Save will/737183 to your computer and use it in GitHub Desktop.
euler38.clj
(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