Last active
January 10, 2023 19:18
-
-
Save plugnburn/c931f72dfd483a431cff75b8674ddf91 to your computer and use it in GitHub Desktop.
An attempt to get a Luhn implementation as small as possible
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
// Input: a string of digits (w/o the check digit) | |
// Output: the check digit | |
// Universal algo - smallest option so far (67 chars after the assignment): | |
luhn=s=>(10-[...s].reverse().reduce((a,v,i)=>a+v*(i&1?1:2.2)|0,0)%10)%10 | |
// IMEI version (14-digit or any other even-digit input) - smallest option so far (57 chars after the assignment): | |
luhnImei=s=>(10-[...s].reduce((a,v,i)=>a+v*(i&1?2.2:1)|0,0)%10)%10 | |
// Credit card version (15-digit or any other odd-digit input) - smallest option so far (57 chars after the assignment): | |
luhnCc=s=>(10-[...s].reduce((a,v,i)=>a+v*(i&1?1:2.2)|0,0)%10)%10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment