Created
January 28, 2014 04:44
-
-
Save trhura/8662380 to your computer and use it in GitHub Desktop.
This file contains 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
(defn palindrome? [string] | |
"Check whether a given string is a palindrome." | |
(loop [string string | |
start 0 | |
end (dec (count string))] | |
(cond | |
;;compare string's start and string's end | |
(not (= (get string start) (get string end))) false | |
;; return true after checking middle char | |
(<= end start) true | |
;; loop inner characters | |
:else (recur string (inc start) (dec end))))) | |
(defn binarystr [num] | |
"Convert a base10 number to base2 number string." | |
(Integer/toString num 2)) | |
(defn double-palindromic? [num] | |
"Check whether a number is palindrome for both bases (base2/base10)." | |
(and (palindrome? (str num)) | |
(palindrome? (binarystr num)))) | |
(defn find-sum-double-palindromics [upto] | |
"Find the sum of all palindromic numbers below `upto'." | |
(reduce + (for [x (range 1 upto) :when (double-palindromic? x)] x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment