Skip to content

Instantly share code, notes, and snippets.

@mnzk
Created December 4, 2011 13:40
Show Gist options
  • Save mnzk/1430210 to your computer and use it in GitHub Desktop.
Save mnzk/1430210 to your computer and use it in GitHub Desktop.
Project Euler 26 by Clojure
(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