A simple Project Euler problem in Clojure
May 6th 2010
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.
Let's first try to generate all the products of two 3 digit numbers:
(defn all-products  (for [x (range 100 1000) y (range 100 1000)] (* x y)))
Now, we have to check whether a number is a palindrome. Here is one way to do it:
(defn reverse-1 [n] (java.math.BigInteger. (apply str (reverse (str n))))) (defn palindrome? [n] (= n (reverse-1 n)))
Just one more step - filter out all palindromes from the product list and find the maximum:
(defn all-palindromes  (filter palindrome? (all-products))) (defn biggest-palindrome  (apply max (all-palindromes)))
The Clojure solution is slow; much slower than a Python version which uses two for loops to compute the products. I am sure that there are better Clojure solutions out there! (there should be a better way to do the number reverse; and, the `apply max' solution for finding maximum of a sequence doesn't sound good).