Math & Trig
Luhn Algorithm (Mod 10) formula: generate and validate account check digits
Generate and check a Luhn checksum for an illustrative eight-digit payload. A passing checksum does not validate an account or identity.
Alternate weights from the rightmost payload digit
Start at the rightmost payload digit with weight 2, then alternate 1 and 2 toward the left. Subtract 9 from doubled values above 9. The eight-digit example therefore uses weights 1,2,1,2,1,2,1,2 from top to bottom.
The check digit makes the weighted total a multiple of ten. Luhn detects single-digit substitutions but not all transpositions: swapping adjacent 09 and 90 can go undetected. It is an error-detection checksum, not fraud detection.
Enter exactly eight integer digits from 0 through 9 and keep the supplied weights. For other payload lengths, regenerate weights from the right; do not simply extend the alternating column. Identifier-specific prefixes and length rules, such as the NPI prefix, are not implemented here.
| Payload Digits [4,5,3,9,1,4,8,8] | Right-to-Left Weights | Luhn Terms IF(p>9, p-9, p) | Check Digit & Validation |
|---|---|---|---|
| 4, 5, 3, 9 | 1, 2, 1, 2 | 4, 1 (10-9), 3, 9 (18-9) | Subtotal = 17 |
| 1, 4, 8, 8 | 1, 2, 1, 2 | 1, 8, 8, 7 (16-9) | Subtotal = 24 (Total S = 41) |
| Check Digit = E2 | Weight = 1 | =MOD(10 - MOD(41,10), 10) | 9 -> Checksum passes for 453914889 (41+9=50) |
Generate Modulo-10 Luhn check digit and validate complete ID
Outer MOD(..., 10) prevents returning a two-character "10" whenever SUM(C2:C9) is already a multiple of 10.
=MOD(10 - MOD(SUM(C2:C9), 10), 10)Check the supplied checksum
E2 must be an integer digit from 0 through 9. TRUE means only that the supplied digits and checksum satisfy the arithmetic rule.
=MOD(SUM(C2:C9) + E2, 10) = 0Try the example
Use the original values or change them to test your understanding.