Skip to content

Instantly share code, notes, and snippets.

@narma
Created August 16, 2012 11:29
Show Gist options
  • Save narma/3369482 to your computer and use it in GitHub Desktop.
Save narma/3369482 to your computer and use it in GitHub Desktop.
clojure-euler-4
(import '(java.lang Math))
(defn palindrome? [n]
(= (->> n str reverse (apply str))
(str n)))
(defn test-for-products [n pmax pmin]
(some #(let [p (/ n %)]
(and (<= p pmax) (>= p pmin)))
(for [i (range pmin (inc pmax)) :let [m (mod n i)] :when (= m 0)] i)))
(defn euler-4-largest-palindrome [max-digits]
(let [max-product (dec (int (Math/pow 10 max-digits)))
min-product (int (Math/pow 10 (dec max-digits)))
]
(first (for [i (range
(* max-product max-product)
(* min-product min-product) -1)
:when (and (palindrome? i)
(test-for-products i max-product min-product))]
i))))
(time (euler-4-largest-palindrome 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment