Skip to content

Instantly share code, notes, and snippets.

@hillerstorm
Forked from 140bytes/LICENSE.txt
Last active December 24, 2015 15:49
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 hillerstorm/6823202 to your computer and use it in GitHub Desktop.
Save hillerstorm/6823202 to your computer and use it in GitHub Desktop.
Calculate checksum using the Luhn Algorithm

Calculate checksum using the Luhn Algorithm

Calculates the checksum of a given input of numbers using the Luhn Algorithm

75bytes

function(
a, // input number sequence as a string
b, // placeholder
c, // --||--
d){ // --||--
c=0;
// traverse the input and multiply every other digit
for(b in a)
d=a[b],c+=
b%2?+d // every other digit is just used straight away
:d*2%10|d/5; // digits above 5 will result in double digits when
// multiplied with 2 and should be added together
//
// 5*2 = 10 -> 1+0 = 1
// 6*2 = 12 -> 1+2 = 3
// 7*2 = 14 -> 1+4 = 5
// 8*2 = 16 -> 1+6 = 7
// 9*2 = 18 -> 1+8 = 9
//
// digits below 5 are just multiplied by 2
// and this, is magic
// multiply the sum by 9 and return the last digit
return c*9%10
}
function(a,b,c,d){c=0;for(b in a)d=a[b],c+=b%2?+d:d*2%10|d/5;return c*9%10}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Johan Hillerström <https://github.com/hillerstorm>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "luhnAlgorithm",
"description": "Calculates a checksum using the Luhn algorithm.",
"keywords": [
"luhn",
"algorithm",
"formula",
"checksum"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>6</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var luhn = function(a,b,c,d){c=0;for(b in a)d=a[b],c+=b%2?+d:d*2%10|d/5;return c*9%10}
document.getElementById('ret').innerHTML = luhn('811218987')
</script>
@atk
Copy link

atk commented Oct 6, 2013

How about

function(a,b,c,d){c=0;for(b in a)d=a[b],c+=b%2?+d:d*2%10+d>4;return c*9%10}

@hillerstorm
Copy link
Author

Doesn't give the same output :o
It does for the example (811218987) but not for e.g. 870810563.

The checksum for the first example should be 6 and for the other one it should be 3

@atk
Copy link

atk commented Oct 13, 2013

I believe this is due to operator precedence.

function(a,b,c,d){c=0;for(b in a)d=a[b],c+=b&1?d*2%10+d>4:+d;return c*9%10}

@atk
Copy link

atk commented Oct 13, 2013

If that also failed, then the modulo has higher precedence than the multiplication and the latter would require brackets.

@atk
Copy link

atk commented Oct 14, 2013

It was operator precedence, but on the comparison rather than the calculation.

function(a,b,c,d){c=0;for(b in a)d=a[b],c+=b%2?+d:d*2%10+(d>4);return c*9%10}

I'll try to find a smaller version without the brackets.

@atk
Copy link

atk commented Oct 14, 2013

A little boolean operator goes a long way :-)

function(a,b,c,d){c=0;for(b in a)d=a[b],c+=b%2?+d:d*2%10|d/5;return c*9%10}

@hillerstorm
Copy link
Author

:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment