Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Created April 26, 2013 01:11
Show Gist options
  • Select an option

  • Save pbalduino/5464486 to your computer and use it in GitHub Desktop.

Select an option

Save pbalduino/5464486 to your computer and use it in GitHub Desktop.
Trying to solve the Euler #04 The sequence way
(ns euler04-2)
(defn peel
"Peel a string like a banana, removing first and last character"
[str]
(butlast (rest str)))
(defn palindrome?
"Returns true or false if name is a palindrome.
This recursive approach is slightly faster than compare with its reverse."
[name]
(let [f (first name)
l (last name)]
(if (or (empty? name) (not= f l))
(= f l)
(recur (peel name)))))
(defn mult [x]
(let [y (range 999 x -1)]
(map #(* % x) y)))
(defn largest-palindrome []
(apply max
(filter #(palindrome? (str %))
(mapcat mult (range 999 99 -1)))))
(defn -main []
(time (println (largest-palindrome))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment