Created
August 16, 2012 11:29
-
-
Save narma/3369482 to your computer and use it in GitHub Desktop.
clojure-euler-4
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
(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