I am doing Project Euler problems as a way to learn better idiomatic Clojure.
Today’s problem really made me realize how cool is clojure. Wording is the following
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.
Easier than expected huh?
In short, is-palindrome just checks for equality between a number and its reversed pair, if they are equal it returns the number, otherwise it returns nil.
The main function uses ‘for’ to multiply two ranges of numbers, i.e: 100*999, 100*998,… 101*999… This returns a lazy sequence that is evaluated against is-palindrome. That will return the same collection but with the value nil on all the numbers that are not palindromes. So we just filter numbers out, get the maximum value of the list of numbers with reduce, and we are all set. Such a great example of how lazy evaluation makes things a lot faster. Any thoughts on how to improve this solution?