Skip to content

Instantly share code, notes, and snippets.

@jramb
Created January 29, 2013 08:04
Show Gist options
  • Save jramb/4662593 to your computer and use it in GitHub Desktop.
Save jramb/4662593 to your computer and use it in GitHub Desktop.
Luhn in Clojure
(defn- digits [n]
(map read-string (re-seq #"[0-9]" (str n))))
(defn luhn? [n]
(zero? (mod
(reduce +
(digits (reduce str
(map * (cycle [1 2]) (reverse (digits n))))))
10)))
(map luhn?
[49927398716 49927398717 1234567812345678 1234567812345670])
;;=> (true false false true)
;; also: http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Clojure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment