Last active
February 13, 2017 08:28
-
-
Save tonsky/f191479f1b49adfcfa9bf2308bd40c77 to your computer and use it in GitHub Desktop.
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
(defn digits [x] | |
(loop [r x, digits []] | |
(if (== 0 r) | |
digits | |
(recur (quot r 10) (conj digits (rem r 10)))))) | |
(defn double-odd-digits [digits] | |
(map-indexed (fn [i d] (if (odd? i) (* 2 d) d)) digits)) | |
(defn valid? [x] | |
(let [sum (->> (digits x) | |
(double-odd-digits) | |
(mapcat digits) | |
(reduce + 0))] | |
(== 0 (rem sum 10)))) | |
(use 'clojure.test) | |
(is (valid? 4012888888881881)) | |
(is (not (valid? 4012888888881882))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment