Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Last active December 12, 2015 10:39
Show Gist options
  • Save loganlinn/4760394 to your computer and use it in GitHub Desktop.
Save loganlinn/4760394 to your computer and use it in GitHub Desktop.
(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