Last active
December 12, 2015 10:39
-
-
Save loganlinn/4760394 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns euler.p4 | |
"A palindromic number reads the same both ways. The largest palindrome made | |
from the product of two 2-digit numbers is 9009 = 91 99. | |
Find the largest palindrome made from the product of two 3-digit numbers.") | |
(defn palindromic? | |
[n] | |
(loop [s (seq (str n))] | |
(if (empty? s) | |
true | |
(if (= (first s) (last s)) | |
(recur (butlast (rest s))) | |
false)))) | |
(defn largest-palindromic-pair | |
[n] | |
(->> | |
(for [x (range 11 n 11) y (range n 1 -1) | |
:let [p (* x y)] | |
:when (palindromic? p)] | |
[p x y]) | |
(apply max-key first) | |
rest)) | |
(time (largest-palindromic-pair 999)) ; => [993 913] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment