Created
December 4, 2011 13:40
-
-
Save mnzk/1430210 to your computer and use it in GitHub Desktop.
Project Euler 26 by Clojure
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
(defn remainders | |
[n m] | |
(let [r (rem n m)] | |
(lazy-seq (cons r (remainders (* r 10) m))))) | |
(defn distance-of-same-value-elements | |
[xs] | |
(loop [i 0, dict {}, xs xs] | |
(let [x (first xs)] | |
(if-let [j (get dict x)] | |
(- i j) | |
(recur (inc i) (assoc dict x i) (rest xs)))))) | |
(def recurring-length | |
(comp distance-of-same-value-elements | |
(partial remainders 1))) | |
(defn euler26 | |
[n] | |
(loop [d n, d-max 0, len-max 0] | |
(if (>= len-max d) | |
d-max | |
(let [len (recurring-length d)] | |
(if (> len len-max) | |
(recur (dec d) d len) | |
(recur (dec d) d-max len-max)))))) | |
(println (euler26 999)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment