encyclopegia.org

Luhn mod N algorithm

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.[4] 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 characters where is divisible by 2.[src]

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.[4] 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 characters where is divisible by 2.[src] ===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).[1] 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.[src] 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.[2] Government ID numbers, bank account numbers, ISBN numbers and many other identification numbers use the Luhn algorithm or a small modification of it.[1] The original Luhn algorithm is a special case of the Luhn mod algorithm, where equals 10.[2] 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.[src] The final digit of this sum should be the same as the final digit of the string.[src] (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.[1] It catches all substitution errors, where one digit is accidentally replaced by another, as well as most transposition errors, with 90 vs.[src] 09 being the sole exception.[src] Although it is a hash function, it is not intended to be cryptographically secure, nor to detect malicious errors or fraud.[src] ===Extending to mod N=== The original Luhn algorithm is called the "mod 10" algorithm because it performs modular arithmetic on a 10-digit system.[2] 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.[2] The same basic idea can be applied to an arbitrary system of N ordered characters.[src] 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.[2] 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.[src] Apart from this distinction, it resembles very closely the original algorithm.[src] 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).[src] The algorithm processes the input string by converting each character to its associated code-point and then performing the computations in mod N (where is the number of valid input characters).[src] Finally, the resulting check code-point is mapped back to obtain its corresponding check character.[src] /// /// This can be any string of characters.[src] /// private const string CodePoints = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private int NumberOfValidInputCharacters() => CodePoints.Length; private int CodePointFromCharacter(char character) => CodePoints.IndexOf(character); private char CharacterFromCodePoint(int codePoint) => CodePoints[codePoint]; The function to generate a check character is: char GenerateCheckCharacter(string input) { int factor = 2; int sum = 0; int n = NumberOfValidInputCharacters(); // Starting from the right and working leftwards is easier since // the initial "factor" will always be "2".[src] for (int i = input.Length - 1; i >= 0; i--) { int codePoint = CodePointFromCharacter(input[i]); int addend = factor * codePoint; // Alternate the "factor" that each "codePoint" is multiplied by factor = (factor == 2) ?[src] 1 : 2; // Sum the digits of the "addend" as expressed in base "n" addend = IntegerValue(addend / n) + (addend % n); sum += addend; } // Calculate the number that must be added to the "sum" // to make it divisible by "n".[src] int remainder = sum % n; int checkCodePoint = (n - remainder) % n; return CharacterFromCodePoint(checkCodePoint); } And the function to validate a string (with the check character as the last character) is: bool ValidateCheckCharacter(string input) { int factor = 1; int sum = 0; int n = NumberOfValidInputCharacters(); // Starting from the right, work leftwards // Now, the initial "factor" will always be "1" // since the last character is the check character.[src] for (int i = input.Length - 1; i >= 0; i--) { int codePoint = CodePointFromCharacter(input[i]); int addend = factor * codePoint; // Alternate the "factor" that each "codePoint" is multiplied by factor = (factor == 2) ?[src] 1 : 2; // Sum the digits of the "addend" as expressed in base "n" addend = IntegerValue(addend / n) + (addend % n); sum += addend; } int remainder = sum % n; return (remainder == 0); } int codePointFromCharacter(char character) {...} char characterFromCodePoint(int codePoint) {...} int numberOfValidInputCharacters() {...} The function to generate a check character is: char generateCheckCharacter(String input) { int factor = 2; int sum = 0; int n = numberOfValidInputCharacters(); // Starting from the right and working leftwards is easier since // the initial "factor" will always be "2".[src] for (int i = input.length() - 1; i >= 0; i--) { int codePoint = codePointFromCharacter(input.charAt(i)); int addend = factor * codePoint; // Alternate the "factor" that each "codePoint" is multiplied by factor = (factor == 2) ?[src] 1 : 2; // Sum the digits of the "addend" as expressed in base "n" addend = (addend / n) + (addend % n); sum += addend; } // Calculate the number that must be added to the "sum" // to make it divisible by "n".[src] int remainder = sum % n; int checkCodePoint = (n - remainder) % n; return characterFromCodePoint(checkCodePoint); } And the function to validate a string (with the check character as the last character) is: boolean validateCheckCharacter(String input) { int factor = 1; int sum = 0; int n = numberOfValidInputCharacters(); // Starting from the right, work leftwards // Now, the initial "factor" will always be "1" // since the last character is the check character.[src] for (int i = input.length() - 1; i >= 0; i--) { int codePoint = codePointFromCharacter(input.charAt(i)); int addend = factor * codePoint; // Alternate the "factor" that each "codePoint" is multiplied by factor = (factor == 2) ?[src] 1 : 2; // Sum the digits of the "addend" as expressed in base "n" addend = (addend / n) + (addend % n); sum += addend; } int remainder = sum % n; return (remainder == 0); } const codePoints = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //This can be any string of permitted characters function numberOfValidInputCharacters() { return codePoints.length; } function codePointFromCharacter(character) { return codePoints.indexOf(character); } function characterFromCodePoint(codePoint) { return codePoints.charAt(codePoint); } The function to generate a check character is: function generateCheckCharacter(input) { let factor = 2; let sum = 0; let n = numberOfValidInputCharacters(); // Starting from the right and working leftwards is easier since // the initial "factor" will always be "2".[src] for (let i = input.length - 1; i >= 0; i--) { let codePoint = codePointFromCharacter(input.charAt(i)); let addend = factor * codePoint; // Alternate the "factor" that each "codePoint" is multiplied by factor = (factor == 2) ?[src] 1 : 2; // Sum the digits of the "addend" as expressed in base "n" addend = (Math.floor(addend / n)) + (addend % n); sum += addend; } // Calculate the number that must be added to the "sum" // to make it divisible by "n".[src] let remainder = sum % n; let checkCodePoint = (n - remainder) % n; return characterFromCodePoint(checkCodePoint); } And the function to validate a string (with the check character as the last character) is: function validateCheckCharacter(input) { let factor = 1; let sum = 0; let n = numberOfValidInputCharacters(); // Starting from the right, work leftwards // Now, the initial "factor" will always be "1" // since the last character is the check character.[src] for (let i = input.length - 1; i >= 0; i--) { let codePoint = codePointFromCharacter(input.charAt(i)); let addend = factor * codePoint; // Alternate the "factor" that each "codePoint" is multiplied by factor = (factor == 2) ?[src] 1 : 2; // Sum the digits of the "addend" as expressed in base "n" addend = (Math.floor(addend / n)) + (addend % n); sum += addend; } let remainder = sum % n; return (remainder == 0); } ==References==[src]

41 sentences · 41 cited inline · 4 references stored

References

  1. Computer for verifying numbers · retrieved 2026-11-17 · rev 1351189129
    Cite this reference

    MLA "Computer for verifying numbers." us expired-lifetime US2950048A , Hans Peter Luhn, "Computer for verifying numbers", published 1960-08-23, issued 1960-08. 2026-11-17. https://patents.google.com/patent/US2950048A/en. Accessed 28 November 2026.

    Chicago "Computer for verifying numbers." us expired-lifetime US2950048A , Hans Peter Luhn, "Computer for verifying numbers", published 1960-08-23, issued 1960-08. 2026-11-17. https://patents.google.com/patent/US2950048A/en.

    APA us expired-lifetime US2950048A , Hans Peter Luhn, "Computer for verifying numbers", published 1960-08-23, issued 1960-08. (2026). Computer for verifying numbers. Retrieved November 28, 2026, from https://patents.google.com/patent/US2950048A/en

  2. Luhn algorithm for Credit Card · retrieved 2026-11-17 · rev 1351189129
    Cite this reference

    MLA "Luhn algorithm for Credit Card." 2026-11-17. https://ambimat.com/luhn-algorithm-for-credit-card/. Accessed 28 November 2026.

    Chicago "Luhn algorithm for Credit Card." 2026-11-17. https://ambimat.com/luhn-algorithm-for-credit-card/.

    APA encyclopegia.org. (2026). Luhn algorithm for Credit Card. Retrieved November 28, 2026, from https://ambimat.com/luhn-algorithm-for-credit-card/

  3. Johansson, Rickard (2014). Developing a knock-out code for production purposes (MSc Technical Design thesis). Lund, Sweden: Lund University. pp. 25, 26 . Retrieved November 7, 2025 . Wikipedia revision 1351189129, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1351189129
    Cite this reference

    MLA "Johansson, Rickard (2014). Developing a knock-out code for production purposes (MSc Technical Design thesis). Lund, Sweden: Lund University. pp. 25, 26 . Retrieved November 7, 2025 . Wikipedia revision 1351189129, retrieved 2026-11-18." Johansson, Rickard (2014). 2026-11-17. https://lup.lub.lu.se/luur/download?func=downloadFile&recordOId=5154159&fileOId=5154173. Accessed 28 November 2026.

    Chicago "Johansson, Rickard (2014). Developing a knock-out code for production purposes (MSc Technical Design thesis). Lund, Sweden: Lund University. pp. 25, 26 . Retrieved November 7, 2025 . Wikipedia revision 1351189129, retrieved 2026-11-18." Johansson, Rickard (2014). 2026-11-17. https://lup.lub.lu.se/luur/download?func=downloadFile&recordOId=5154159&fileOId=5154173.

    APA Johansson, Rickard (2014). (2026). Johansson, Rickard (2014). Developing a knock-out code for production purposes (MSc Technical Design thesis). Lund, Sweden: Lund University. pp. 25, 26 . Retrieved November 7, 2025 . Wikipedia revision 1351189129, retrieved 2026-11-18. Retrieved November 28, 2026, from https://lup.lub.lu.se/luur/download?func=downloadFile&recordOId=5154159&fileOId=5154173

  4. What is the Luhn algorithm and how does it work? | Stripe · retrieved 2026-11-17 · rev 1351189129
    Cite this reference

    MLA "What is the Luhn algorithm and how does it work? | Stripe." a b c "What is the Luhn algorithm and how does it work? | Stripe". 2026-11-17. https://stripe.com/resources/more/how-to-use-the-luhn-algorithm-a-guide-in-applications-for-businesses. Accessed 28 November 2026.

    Chicago "What is the Luhn algorithm and how does it work? | Stripe." a b c "What is the Luhn algorithm and how does it work? | Stripe". 2026-11-17. https://stripe.com/resources/more/how-to-use-the-luhn-algorithm-a-guide-in-applications-for-businesses.

    APA a b c "What is the Luhn algorithm and how does it work? | Stripe". (2026). What is the Luhn algorithm and how does it work? | Stripe. Retrieved November 28, 2026, from https://stripe.com/resources/more/how-to-use-the-luhn-algorithm-a-guide-in-applications-for-businesses

Cite this article

MLA

"Luhn mod N algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/luhn-mod-n-algorithm. Accessed 28 November 2026.

Chicago

"Luhn mod N algorithm." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/luhn-mod-n-algorithm.

APA

encyclopegia.org. (2026). Luhn mod N algorithm. Retrieved November 28, 2026, from https://encyclopegia.org/wiki/luhn-mod-n-algorithm

Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · tier1-factual · https://en.wikipedia.org/w/index.php?oldid=1351189129 · 0.55 · snapshot luhn-mod-n-algorithm-758154-21f56e5b9ce45894 · QID Q6699881

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners
  • Index — every corpus, paper and record
  • Estate guide — what a public data estate is
  • Founder — Lois-Kleinner Alpasan
  • Records — DOIs and external deposits
  • Documents — one-pagers, PDFs and QR codes
  • Brand kit — logos and share images
  • Papers — plain-language briefs with citations
  • Browser — search engine, PWA and bookmarklet
  • Anticloud — the operator and its projects

As seen in Business Insider · 336 outlets · evidence DOIs · plain-language papers

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.