Skip to content

Instantly share code, notes, and snippets.

@tonsky
Last active February 13, 2017 08:28
Show Gist options
  • Save tonsky/f191479f1b49adfcfa9bf2308bd40c77 to your computer and use it in GitHub Desktop.
Save tonsky/f191479f1b49adfcfa9bf2308bd40c77 to your computer and use it in GitHub Desktop.
(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