Luhn algorithm: Difference between revisions

Content deleted Content added
Section: Uses
TDLMTH (talk | contribs)
The original formula "10 - (s mod 10)" returns the range 1-10, not 0-9, and so would cause the check digit calculation to fail; the revised formula accounts for this. The new "other valid formula" gives the same result and is arguably cleaner. The note addresses environments (e.g., the Java programming language) that handle modulo of negative numbers differently.
Line 11:
# With the payload, start from the rightmost digit. Moving left, double the value of every second digit (including the rightmost digit).
# Sum the values of the resulting digits.
# The check digit is calculated by <math>(10 - (s \operatorname{mod} 10)) \operatorname{mod} 10</math>. This is the least number (possibly zero) that must be added to <math>s</math> to make a multiple of 10. Other valid formulas giving the same value are <math>9 - ((s + 9)\operatorname{mod} 10)</math>, <math>(10 - s)\operatorname{mod} 10</math>, and <math>10\lceil s/10\rceil - s</math>. Note that the formula <math>(10 - s)\operatorname{mod} 10</math> will not work in all environments due to differences in how negative numbers are handled by the modulo operation<ref>https://en.wikipedia.org/wiki/Modulo</ref>.
 
=== Example for computing check digit ===