Skip to content

Instantly share code, notes, and snippets.

Created September 27, 2013 01:31
Show Gist options
  • Save anonymous/6723019 to your computer and use it in GitHub Desktop.
Save anonymous/6723019 to your computer and use it in GitHub Desktop.
; This gist relates to Misophistful's video "Understanding list comprehension in Clojure"
; http://www.youtube.com/watch?v=5lvV9ICwaMo
(defn palindrome? [n]
(let [s (str n)]
(= s (clojure.string/reverse s))))
(def palindromes
(for [n1 (range 100 1000)
n2 (range 100 1000)
:let [product (* n1 n2)]
:when (palindrome? product)
{ :factors [n1 n2] :product product }) ; <--- generate a compound value for each palindrome
; This generates a sequence of maps:
; ( {:factors [101 101], :product 10201}
; {:factors [101 111], :product 11211}
; {:factors [101 121], :product 12221})
; ...
; )
; Now we can find the largest palindrome _and_ the numbers that made it.
(last (sort-by :product palindromes))
;= {:factors [993 913], :product 906609}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment