Luhn check

A simple checksum formula whose last digit lets software spot mistyped card numbers and other identification numbers.

In more detail

Starting from the right, every second digit is doubled, nine is subtracted from any result above nine, and all the digits are added; a valid number gives a total that is a multiple of ten. It catches any single wrong digit and most swaps of neighbouring digits, but it is not security, since anyone can compute it. Card number scanners use it to weed out false alarms, such as order and phone numbers of the right length.

Examples

def luhn_ok(digits): ... return total % 10 == 0
line 1: 773022******4408  fails Luhn, not a card

Related terms

Taught in

References

Certification courses