ArticleslgStudy

computer science

Luhn mod N algorithm

Luhn mod N algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Luhn mod N algorithm rather than just read about it. In short: The Luhn mod N algorithm is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of values in any even-numbered base. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or any arbitrary set of N characters where N is divisible by 2.

Key takeaways

  • Luhn mod N algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Luhn mod N algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Luhn mod N algorithm from memory before moving on to harder problems.

Reference excerpt

The Luhn mod N algorithm is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of values in any even-numbered base. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of letters and digits or any arbitrary set of N characters where N is divisible by 2.

Informal explanation

Original algorithm (Luhn mod 10) The original Luhn algorithm was designed by Hans Peter Luhn and patented in 1960 (as a physical device for implementing the algorithm). It is a check digit algorithm that is intended to catch most common input errors by checking the algorithm's calculation of the digits of an identification number against the final ("check") digit. One particularly common use of the Luhn algorithm is in credit cards; the final digit of a 16-digit credit card number is the output of the Luhn algorithm applied to the first 15 digits. Government ID numbers, bank account numbers, ISBN numbers and many other identification numbers use the Luhn algorithm or a small modification of it. The original Luhn algorithm is a special case of the Luhn mod N algorithm, where N equals 10. It takes in a string of digits (omitting the final digit), reverses the string, multiplies every other digit by two, then sums all of them. The final digit of this sum should be the same as the final digit of the string. (For a full example of this calculation, see Luhn algorithm#Description.) The goal of the Luhn algorithm is to detect common input errors when entering a string of digits into a computer. It catches all substitution errors, where one digit is accidentally replaced by another, as well as most transposition errors, with 90 vs. 09 being the sole exception. Although it is a hash function, it is not intended to be cryptographically secure, nor to detect malicious errors or fraud.

Extending to mod N The original Luhn algorithm is called the "mod 10" algorithm because it performs modular arithmetic on a 10-digit system. The check digit is generated by summing up the Luhn algorithm and taking the result modulo 10, which is equivalent to the remainder left over when dividing by 10, or the "ones" digit of the number. The same basic idea can be applied to an arbitrary system of N ordered characters. The Luhn mod N algorithm generates a check digit (more precisely, a check character) within the same range of valid characters as the input string. For example, if the algorithm is applied to a string of lower-case letters (a to z), the check character will also be a lower-case letter. Apart from this distinction, it resembles very closely the original algorithm. The main idea behind the extension is that the full set of valid input characters is mapped to a list of code-points (i.e., sequential integers beginning with zero). The algorithm processes the input string by converting each character to its associated code-point and then performing the computations in mod N (where N is the number of valid input characters). Finally, the resulting check code-point is mapped back to obtain its corresponding check character.

Limitation The Luhn mod N algorithm only works where N is divisible by 2. This is because there is an operation to correct the value of a position after doubling its value which does not work where N is not divisible by 2. For applications using the ISO basic Latin alphabet this is not a problem, since a string of same-case letters has 26 code-points. Adding decimal characters adds a further 10, and adding the other case adds a further 26, maintaining an N divisible by 2 in both cases.

Explanation The second step in the Luhn algorithm re-packs the doubled value of a position into the original digit's base by adding together the individual digits in the doubled value when written in base N. This step results in even numbers if the doubled value is less than or equal to N, and odd numbers if the doubled value is greater than N. For example, in decimal applications where N is 10, original values between 0 and 4 result in even numbers and original values between 5 and 9 result in odd numbers, effectively re-packing the doubled values between 0 and 18 into a single distinct result between 0 and 9. Where an N is used that is not divisible by 2 this step returns even numbers for doubled values greater than N which cannot be distinguished from doubled values less than or equal to N.

Outcome The algorithm will neither detect all single-digit errors nor all transpositions of adjacent digits if an N is used that is not divisible by 2. As these detection capabilities are the algorithm's primary strengths, the algorithm is weakened almost entirely by this limitation. The Luhn mod N algorithm odd variation enables applications where N is not divisible by 2 by replacing the doubled value at each position with the remainder after dividing the position's value by N which gives odd number remainders consistent with the original algorithm design.

Mapping characters to code-points Initially, a mapping between valid input characters and code-points must be created. For example, consider that the valid characters are the lower-case letters from a to f. Therefore, a suitable mapping would be:

Note that the order of the characters is completely irrelevant. This other mapping would also be acceptable (although possibly more cumbersome to implement):

It is also possible to intermix letters and digits (and possibly even other characters). For example, this mapping would be appropriate for lower-case hexadecimal digits:

Algorithm in C# Assuming the following functions are defined:

The function to generate a check character is:

And the function to validate a string (with the check character as the last character) is:

Algorithm in Java Assuming the following functions are defined:

The function to generate a check character is:

And the function to validate a string (with the check character as the last character) is:

Algorithm in JavaScript Assuming the following functions are defined:

The function to generate a check character is:

And the function to validate a string (with the check character as the last character) is:

Example

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Luhn mod N algorithm

Start with the simplest possible case. Write down what Luhn mod N algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Luhn mod N algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Luhn mod N algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Luhn mod N algorithm

In research
Luhn mod N algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Luhn mod N algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Luhn mod N algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Checksum algorithms, Modular arithmetic, so understanding it makes those chapters shorter.
In everyday life
Look for Luhn mod N algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Luhn mod N algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Luhn mod N algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Luhn mod N algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Luhn mod N algorithm in simple terms?

The Luhn mod N algorithm is an extension to the Luhn algorithm (also known as mod 10 algorithm) that allows it to work with sequences of values in any even-numbered base. This can be useful when a check digit is required to validate an identification string composed of letters, a combination of let…

Why does Luhn mod N algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Luhn mod N algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Luhn mod N algorithm.

Tags

  • Checksum algorithms
  • Modular arithmetic

Keep exploring