Created
April 27, 2016 22:24
-
-
Save benley/cd22b6b45729cef58f77fdf93a0c1e92 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
| local toInt(x) = if x > (std.floor(x) + 0.5) then std.floor(x) + 1 else std.floor(x); | |
| local sum(xs) = std.foldl(function(a,b) a+b, xs, 0); | |
| local headn(x) = std.floor(x/10); | |
| local tailn(x) = toInt(((x/10) - headn(x)) * 10); | |
| local sumdigits(x) = if x < 10 then x else sumdigits(headn(x)) + tailn(x); | |
| local splitrev(x) = if x<10 then [x] else [tailn(x)] + splitrev(headn(x)); | |
| local luhn(x) = | |
| local xs = splitrev(x); | |
| (sum(xs[::2]) + sum([sumdigits(n*2) for n in xs[1::2]])) % 10 == 0; | |
| local ccnums = [ 49927398716, 49927398717, 1234567812345678, 1234567812345670 ]; | |
| std.assertEqual([luhn(x) for x in ccnums], [true, false, false, true]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment