Created
April 26, 2013 01:11
-
-
Save pbalduino/5464486 to your computer and use it in GitHub Desktop.
Trying to solve the Euler #04 The sequence way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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