Skip to content

Instantly share code, notes, and snippets.

@snt
Created April 17, 2014 06:51
Show Gist options
  • Save snt/10958854 to your computer and use it in GitHub Desktop.
Save snt/10958854 to your computer and use it in GitHub Desktop.
Luhn algorithm checker
(ns cl1.core)
(defn prod-digits
[ps]
(->> ps
(map #(reduce * %1))
(map #(cond
(> %1 9) (- %1 9)
:else %1))
(reduce +)))
(defn split-string-to-letters
[ss]
(map (comp #(Integer/parseInt %) str) ss))
(defn luhn?
"Check Luhn check digit"
[n]
(->> n
Integer/toString
split-string-to-letters
(map vector (cycle [2 1]))
prod-digits
(#(mod % 10))
(= 0) ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment