Created
April 17, 2014 06:51
-
-
Save snt/10958854 to your computer and use it in GitHub Desktop.
Luhn algorithm checker
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
(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