The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.
The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 78121^{[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 cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from collections of random digits.
Contents 
The Luhn algorithm will detect any singledigit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the twodigit sequence 09 to 90 (or vice versa). It will detect 7 of the 10 possible twin errors (it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).
Other, more complex checkdigit algorithms (such as the Verhoeff algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports nonnumerical strings.
Because the algorithm operates on the digits in a righttoleft manner and zero digits affect the result only if they cause shift in position, zeropadding the beginning of a string of numbers does not affect the calculation. Therefore, systems that normalize to a specific number of digits by converting 1234 to 00001234 (for instance) can perform Luhn validation before or after the normalization and achieve the same result.
The algorithm appeared in a US Patent for a handheld, mechanical device for computing the checksum. It was therefore required to be rather simple. 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.
The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:
As an illustration, if the account number is 49927398716, it will be validated as follows:
Some credit cards use the "Mod 10 plus 5" variant to extend the space of valid card numbers.^{[citation needed]} In this variant, if the sum ends in 0 or 5, the number is considered valid.
Python variant:
def is_mod10(cc): dub, tot = 0, 0 for i in range(len(cc)  1, 1, 1): for c in str((dub + 1) * int(cc[i])): tot += int(c) dub = (dub + 1) % 2 return (tot % 10) == 0
Ruby variant:
def check_luhn(s) tot = 0 s.reverse.split(//).each_with_index do d, i d = d.to_i tot += i + 1 % 2 == 1 ? d : d > 4 ? d * 2 % 10 + 1 : d * 2 end tot % 10 == 0 end
Java variant:
public static boolean isValidCC(String num) { final int[][] sumTable = {{0,1,2,3,4,5,6,7,8,9},{0,2,4,6,8,1,3,5,7,9}}; int sum = 0, flip = 0; for (int i = num.length()  1; i >= 0; i, flip++) sum += sumTable[flip & 0x1][Character.digit(num.charAt(i), 10)]; return sum % 10 == 0; }
C# variant:
public static int CalculateCheckDigit(string value) { int ConSum = 0; bool doubleDigit = true; // check digit is absent before generation for (int i = value.Length  1; i >= 0; i) { int digit = 0; Int32.TryParse(value[i].ToString(), out digit); int ConAdd = digit * (doubleDigit? 2: 1); doubleDigit = !doubleDigit; ConSum += (ConAdd > 9 ? ConAdd = 9 : ConAdd); } return ((ConSum % 10) == 0 ? 0 : (10  (ConSum % 10))); }
JavaScript variant:
var cardValid = function( cardNo ){ var sum = 0, iNum; for( var i = cardNo.length1; i>=0; i ){ iNum = parseInt(cardNo[i]); sum += i%2?iNum:iNum>4?iNum*2%10+1:iNum*2; } return !(sum%10); };
Delphi variant:
function LuhnCheckDigit(const AInputString: string): Byte; var i: Integer; LSum: Integer; LDigit: Integer; begin i := 0; LSum := 0; while i < Length(AInputString) do begin LDigit := StrToInt(AInputString[Length(AInputString)  i]); LDigit := LDigit * (1 + (i mod 2)); LSum := LSum + (LDigit div 10) + (LDigit mod 10); inc(i); end; Result := LSum mod 10; end;
Excel variant:
=RIGHT((LEFT(A1)+MID(A1;3;1)+MID(A1;5;1)+MID(A1;8;1)+MID(A1;10;1))*2((LEFT(A1)*1>4)+(MID(A1;3;1)*1>4)+(MID(A1;5;1)*1>4)+(MID(A1;8;1)*1>4)+(MID(A1;10;1)*1>4))*9+MID(A1;2;1)+MID(A1;4;1)+MID(A1;6;1)+MID(A1;9;1)+RIGHT(A1))="0"
Can be used for condition formating, returns TRUE or FALSE.
checksum
method)
