Last active
November 28, 2015 19:59
-
-
Save gfmurphy/a270f1c28863993645e1 to your computer and use it in GitHub Desktop.
Readability
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 digits | |
"Generate a list of digits contained in the number" | |
[number] | |
(loop [found-digits '() base (quot number 10) digit (rem number 10)] | |
(let [found-digits (conj found-digits (if (neg? digit) (- digit) digit))] | |
(if (zero? base) | |
found-digits | |
(recur found-digits (quot base 10) (rem base 10)))))) | |
(defn divisible-digits | |
"Return the count of digits that evenly divide into the given number" | |
[number] | |
(let [divisible (filter (fn [n] (and (not (zero? n)) (= 0 (rem number n)))) (digits number))] | |
(count divisible))) | |
(map divisable-digits '(1024 2048 64)) |
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
digits n = | |
findDigits (rem n 10) [] (quot n 10) | |
where addDigit d ds = (abs d): ds | |
findDigits d ds 0 = addDigit d ds | |
findDigits d ds b = findDigits (rem b 10) (addDigit d ds) (quot b 10) | |
divisableDigits n = | |
length (filter divisable (digits n)) | |
where zero 0 = True | |
zero _ = False | |
divisable d | zero d = False | |
divisable d | not (zero d) = mod n d == 0 | |
map divisableDigits [1024, 2048, 64] |
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
def digits(base) | |
[].tap { |digits| | |
begin | |
base, digit = base.divmod(10) | |
digits << digit | |
end while base != 0 | |
} | |
end | |
def divisible_digits(number) | |
digits(number).count { |d| !d.zero? && (number % d == 0) } | |
end | |
[1024, 2048, 64].map { |n| divisible_digits(n) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Haskell version is by far the most interesting to me. It's compact, clear and typesafe. There are also no conditionals. They're all eliminated by pattern matching.