HOME

TheInfoList



OR:

The Luhn algorithm or Luhn formula (creator:
IBM International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
scientist
Hans Peter Luhn Hans Peter Luhn (July 1, 1896 – August 19, 1964) was a German-American researcher in the field of computer science and Library & Information Science for IBM, and creator of the Luhn algorithm, KWIC (Key Words In Context) indexing, and s ...
), also known as the " modulus 10" or "mod 10"
algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
, is a simple
check digit A check digit is a form of redundancy check used for Error detection and correction, error detection on identification numbers, such as bank account numbers, which are used in an application where they will at least sometimes be input manually. It ...
formula used to validate a variety of identification numbers. The algorithm is in the
public domain The public domain (PD) consists of all the creative work to which no Exclusive exclusive intellectual property rights apply. Those rights may have expired, been forfeited, expressly Waiver, waived, or may be inapplicable. Because no one holds ...
and is in wide use today. It is specified in ISO/IEC 7812-1. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit card numbers and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.


Description

The check digit is computed as follows: # Drop the check digit from the number (if it's already present). This leaves the payload. # Start with the payload digits. Moving from right to left, double every second digit, starting from the last digit. If doubling a digit results in a value > 9, subtract 9 from it (or sum its digits). # Sum all the resulting digits (including the ones that were not doubled). # The check digit is calculated by (10 - (s \bmod 10)) \bmod 10, where s is the sum from step 3. This is the smallest number (possibly zero) that must be added to s to make a multiple of 10. Other valid formulas giving the same value are 9 - ((s + 9)\bmod 10), (10 - s)\bmod 10, and 10\lceil s/10\rceil - s. Note that the formula (10 - s)\bmod 10 will not work in all environments due to differences in how negative numbers are handled by the
modulo In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the '' modulus'' of the operation. Given two positive numbers and , mo ...
operation.


Example for computing check digit

Assume an example of an account number 1789372997 (just the "payload", check digit not yet included): The sum of the resulting digits is 56. The check digit is equal to (10 - (56 \bmod 10))\bmod 10 = 4. This makes the full account number read 17893729974.


Example for validating check digit

# Drop the check digit (last digit) of the number to validate. (e.g. 17893729974 → 1789372997) # Calculate the check digit (see above) # Compare your result with the original check digit. If both numbers match, the result is valid.


Strengths and weaknesses

The Luhn algorithm will detect all single-digit errors, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence ''09'' to ''90'' (or vice versa). It will detect most of the possible twin errors (it will not detect ''22'' ↔ ''55'', ''33'' ↔ ''66'' or ''44'' ↔ ''77''). Other, more complex check-digit algorithms (such as the Verhoeff algorithm and the Damm algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports non-numerical strings. Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that pad to a specific number of digits (by converting 1234 to 0001234 for instance) can perform Luhn validation before or after the padding and achieve the same result. The algorithm appeared in a United States Patent for a simple, hand-held, mechanical device for computing the checksum. The device took the mod 10 sum by mechanical means. The ''substitution digits'', that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.


Pseudocode implementation

The following function takes a card number, including the check digit, as an array of integers and outputs true if the check digit is correct, false otherwise. function isValid(cardNumber ..length sum := 0 parity := length mod 2 for i from 1 to (length - 1) do if i mod 2 != parity then sum := sum + cardNumber elseif cardNumber > 4 then sum := sum + 2 * cardNumber - 9 else sum := sum + 2 * cardNumber end if end for return cardNumber ength

((10 - (sum mod 10)) mod 10) end function


Uses

The Luhn algorithm is used in a variety of systems, including: * Credit card numbers * IMEI numbers *
CUSIP A CUSIP () is a nine-character numeric or alphanumeric code that uniquely identifies a North American financial security (finance), security for the purposes of facilitating Clearing (finance), clearing and settlement (finance), settlement of tr ...
numbers for North American financial instruments * National Provider Identifier numbers in the United States *
Canadian Canadians () are people identified with the country of Canada. This connection may be residential, legal, historical or cultural. For most Canadians, many (or all) of these connections exist and are collectively the source of their being ''C ...
social insurance numbers * Israeli ID numbers * South African ID numbers * South African Tax reference numbers * Swedish
national identification number A national identification number or national identity number is used by the governments of many countries as a means of uniquely identifying their citizens or residents for the purposes of work, taxation, government benefits, health care, bank ...
s * Swedish Corporate Identity Numbers (OrgNr) *
Greek Greek may refer to: Anything of, from, or related to Greece, a country in Southern Europe: *Greeks, an ethnic group *Greek language, a branch of the Indo-European language family **Proto-Greek language, the assumed last common ancestor of all kno ...
Social Security Numbers (ΑΜΚΑ) * ICCID of SIM cards * European patent application numbers * Survey codes appearing on
McDonald's McDonald's Corporation, doing business as McDonald's, is an American Multinational corporation, multinational fast food chain store, chain. As of 2024, it is the second largest by number of locations in the world, behind only the Chinese ch ...
,
Taco Bell Taco Bell Corp. is an American multinational chain of fast food restaurants founded in 1962 by Glen Bell (1923–2010) in Downey, California. Taco Bell is a subsidiary of Yum! Brands, Inc. The restaurants serve a variety of Mexican-inspired ...
, and Tractor Supply Co. receipts *
United States Postal Service The United States Postal Service (USPS), also known as the Post Office, U.S. Mail, or simply the Postal Service, is an independent agencies of the United States government, independent agency of the executive branch of the federal governmen ...
package tracking numbers use a modified Luhn algorithm * Italian VAT numbers ( Partita Iva)


References


Notes


External links


Luhn test of credit card numbers
on
Rosetta Code Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms and solutions to various computer programming, programming problems in many different programming languages. It is named for the Rosetta Stone ...
: Luhn algorithm/formula implementation in 160 programming languages {{DEFAULTSORT:Luhn Algorithm Modular arithmetic Checksum algorithms Error detection and correction 1954 introductions Articles with example pseudocode Management cybernetics