Skip to content

Instantly share code, notes, and snippets.

@plugnburn
Last active January 10, 2023 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugnburn/c931f72dfd483a431cff75b8674ddf91 to your computer and use it in GitHub Desktop.
Save plugnburn/c931f72dfd483a431cff75b8674ddf91 to your computer and use it in GitHub Desktop.
An attempt to get a Luhn implementation as small as possible
// 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