Talk:Luhn algorithm

message: The only problem with this is that you get the same answer whether you start on the right hand side or the left hand side. It might be good to use a PAN with an even number of digits.   —Preceding unsigned comment added by 125.236.50.155 (talk) 22:57, 27 November 2008 (UTC)
thread_title:  Worked Example 

message: http://www.pat2pdf.org/patents/pat2950048.pdf 
Page 4, Point 20 ~ 25
With numbers that result in 10 or greater are added together, not subtract 9.  However when you are write computer code minus 9 is normally used.
ممكن اعرف انت في حياتي  Maommad (talk) 11:51, 6 January 2021 (UTC)
thread_title:  Patent 2950048 

message: Is this algorithm no longer valid?  There are now VISA cards in circulation that fail this test but are legitimate cards.
May well be. In at least one country, Visa uses the last 3 digits (of 16) as a serial number for the same customer. So it may well be that we have to consider only the first 13 digits.  —Preceding unsigned comment added by 117.193.104.83 (talk) 04:19, 4 May 2011 (UTC)
thread_title:  No longer valid? 

message: The Luhn formula will catch any single-digit error.  It will also catch almost any transposition of adjacent digits (e.g., 18 <-> 81).  There is one adjacent-digit transposition that Luhn will not catch:  09 <-> 90.  It would be a prudent step for applications using Luhn to refrain from issuing numbers with 09 and/or 90 in them, so that this particular transposition cannot result in another valid number.  The Verhoeff check digit formula (not currently described in Wikipedia) might be a better choice, since it catches all adjacent-digit transposition errors. Richwales 21:17, 14 April 2006 (UTC)
replies: text: The Verhoeff algorithm is described now. Richwales 00:06, 20 April 2006 (UTC)
thread_title:  Strengths/Weaknesses 

message: The algorithm stated checks if a number is valid according to the Luhn algorithm, but does anyone have an algorithm that computes a Luhn check digit, i. e. modifies an existing number by appending a digit so that the result is Luhn valid? I mean, something smarter than trying the ten possible digits until the matching one is found. - wr 10-oct-2006
:Agreed, but the code for this would be so similar to the CreateNumber example as to get repetitive (based on idea that this is not code repository).  Would it be more relevant to replace CreateNumber with GetCheckDigit?  Probably best to just go with one or the other. Would be happy to post calc function if deemed more relevant than existing CreatNumber. Skratowicz 03:31, 29 June 2007 (UTC)
replies: text: The algorithm to create the check digit the same algorithm, with an extra step at the end. Put 0 in as a temporary placeholder for the check digit, then calculate the sum (as explained in the article). Calculate the sum modulus 10. If the result is 0, then you're done - the check digit is zero. Otherwise, the check digit is 10 - (sum modulus 10). Using the example from the article, we start with the base account number of 499273987x, where x is the check digit. Substitute zero for x, and the calculations are: 0 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 64 (compare to step 2 in the main article's illustration). 64 modulus 10 is 4, so the check digit is 10-4, or 6. 205.210.222.150 (talk) 14:15, 16 April 2009 (UTC)
thread_title:  Computing the check digit 

message: This article was just plain wrong in the way it was worded.  It talked about "calculating" a checksum digit, but explained how to VERIFY a checksum digit.  Two slightly different tasks.  I changed the text to match the algorithm since that is the most commonly used purpose.  I tried to remove everything that didn't make sense.  If some one really wants to add back in an area for calculating the checksum digit, by all means do so, but please be precise and clear.
Responses to various Talk Topics:
Computing the check digit is a simple excercise of mathematics, and a well written Luhn Algorithm will be able to do it with little stress.  Implementation is left as an excersize.
The Mod 10 (Luhn) algorithm is very much valid.  If you know of any Visa numbers that do not pass it, please elaborate, but my business depends on it, and there is no notice of which I am aware which depricates this algorithm.
The current state of the pseudocode is still wrong IMHO, and at least one other person agrees.  The algorithm, as it is stated, goes from the end to the beginning, so make the pseudo-code loop go from the end to the beginning.  Don't ever bother checking for this parity value that only stands to confuse the reader....if the pseudo-code goes from right to left with if statements, restate the algorithm to do the same.  I have seen far too many implementations that take this crazy turn:(  Also, ADD SOME CURLY BRACES!!!!  Allow me to paraphrase Larry Wall:  "Even if you aren't in doubt, consider the mental welfare of the person who has to read the code after you, and who will probably put braces in the wrong place."
Before any one tells me to be bold, please don't.  Mod 10 is part of my business.  As much as I would like to help the world understand the algorithm, I cannot share my knowledge without giving away my business' IP.  I am glad to remove inaccuracies and to point others in the right direction, but creation of any specific code (pseudo or real) can get me into trouble.  Sorry. :( --dmprantz 17 October 2006 (UTC)
: Anyone else surprised by the notion that someone's living hinges on having superior knowledge of how to implement Mod 10 decimal digit sums. I'd love to see a description of what this kind of job *is* in the article.  — Preceding unsigned comment added by Xcbsmith (talk • contribs) 21:54, 15 November 2011 (UTC)
thread_title:  Comments on Changes 

message: The VB/VB.net algorithm was not valid vb.net. Both algorithms introduced extraneous complexities that utilize more concepts than necessary to demonstrate the Luhn algorithm. Wikipedia is an encyclopedia, not a code repository, and for this reason, these two extra algorithms have been removed. The C# algorithm is the most straightforward and does not introduce unnecessary concepts. The C# algorithm is also consistent in its coding to the following algorithm that generates Luhn sequences. ZX2C4 08:05, 6 January 2007 (UTC)
thread_title:  Removal of VB and PHP Algorithms 

message: Many people must be interested in links to the algorithms! As a compromise, let's just keep them on the discussion page, shall we?
PLEASE don't delete these! Most of us don't care for C#! Paperweight 21:18, 28 February 2007 (UTC)
thread_title:  Algorithms 

message: Still missing this one :)
 LuhnCheck[n_] := Module[{digits = IntegerDigits[n]},
  For[i = Length[digits] - 1, i >= 1, i -= 2, digitsi =  FixedPoint[Composition[Total, IntegerDigits], 2*digitsi]];
  Mod[Total[digits], 10] == 0
 ]
11:42, 28 December 2007 (UTC)
thread_title:  Mathematica implementation 

message: There are at least two errors in the javascript implementation linked to in the article here.  The 'if' check to see if a digit is longer than 9 "if( clen [n] > 9 ) clen [n]-=9;" should be inside of the preceding 'for' loop or in it's own or else it doesn't do anything and there is a closing curly bracket that ends the function before the function is complete (line 13 in the linked to code).  I'm not sure about the other implementations but this one is wrong so I have removed it until it can be replaced by a correct implementation.  —Preceding unsigned comment added by 24.207.187.11 (talk) 07:08, 14 September 2008 (UTC)
thread_title:  JavaScript implementation is wrong 

message: I noticed that the current link to the Implementation in Scheme, although a working implementation, is not a particularly Scheme-like example in that it uses iteration and variable mutation rather than tail-recursion which is far more typical of a Scheme function. Can I propose that we include a link to my own implementation which is tail-recursive. This will give Schemers a choice of the two implementations. Unfortunately, I edited the page to include my own link but found that it had been reverted because it's on a blogspot URL so was told to post a note here instead.  —Preceding unsigned comment added by 62.189.169.182 (talk) 09:12, 7 November 2008 (UTC)
thread_title:  Implementation in Scheme 

message: The code provided as the Excel example does not work in Microsoft Excel 2008 for Mac.  I've commented it out in the article but left the code intact.  I apologize if this is not the correct way to deal with this issue. Can this code please be checked for accuracy?
Thank you.
Christofer C. Bell (talk) 01:50, 14 March 2010 (UTC)
thread_title:  Excel code does not work in Excel 2008 for Mac 

message: Can anyone check the ruby algorithm? It clearly fails on validating some valid credit cards where the JS version returns true.
--85.179.130.223 (talk) 16:49, 1 April 2010 (UTC)
thread_title:  Error in Ruby Algorithm 

message: If a digit 9 is to be doubled, the example implementation will add in (9 * 2) % 9, or 0.  The actual algorithm would add a 9.  Given that the final sum is taken mod 10, it'll be 1 too high for every doubled 9 in the original number.
I've fixed this.  It somewhat complicates the code, but it's clearly incorrect otherwise.  98.111.85.57 (talk) 17:01, 3 May 2010 (UTC)
thread_title:  Example implementation incorrect 

message: I copied the source code from: https://sites.google.com/site/abapexamples/javascript/luhn-validation
And the author allow me to remove the credit. (for wiki).  —Preceding unsigned comment added by 79.181.25.30 (talk) 21:30, 15 January 2011 (UTC)
thread_title:  JavaScript implementation added, with permission. 

message: I really doubt that the 10+5 variant would actually be used somewhere... First, why would it be called 10+5 instead of "mod 5"? Second, why would someone use a much worse checksum -- I don't think the nice properties of the Luhn algorithm transfer to the mod 5 variant. Third, what would be the reason for not using the original Luhn?
I suggest removal or a reference to some proof... 62.78.167.76 (talk) 13:48, 23 January 2011 (UTC)
thread_title:  10+5 ? 

message: More implementations of the algorithm are not needed in the article, as Wikipedia is an encyclopedia and not a programming tutorial. Here's what the draft of the WikiProject Computer Science manual of style has to say about the number of code samples:
Multiple source code implementations are not appropriate unless they contrast specific aspects of the code and that contrast is important to the encyclopedic content of the article. If possible, accentuate differences by providing the alternate implementation in the same language as the original.
– Acdx (talk) 03:14, 7 February 2011 (UTC)
replies: text:  - Agreed.  C is far too verbose, unclear, and distracting... Fortran and PHP are not really relevant here - I doubt anyone is implementing something like this in either language.  I've removed them leaving Java, Python, and Groovy, with justifications for each.  Java is obviously a mainstream language which many will look for, Python implements the algorithm very concisely, and Groovy is a good balance between the two, and I feel illustrates the algorithm without any distractions (i.e. the sumTable in Java or the slice operator in Python).  I think between the 3 the algorithm is properly demonstrated. (A C# implementation is probably the only thing missing) RebelBodhi (talk) 03:20, 9 May 2011 (UTC)
replies: text:  - I disagree with the assessment on PHP.  Removing C and Fortran seems valid, but PHP is in fact one of the most popular programming languages on the web and is used by a large number of e-commerce sites.  Computing Luhn in PHP is not at all uncommon and it is instructive to see the novel approach that was used here. Isnoop (talk) 00:27, 18 May 2011 (UTC)
thread_title:  Implementations 

message: Both the C# implementation and the Scala implementation seems wrong to me. The algorithm states "Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit". Then, shouldn't the example read:
bool isLuhnValid(string s)
{
    return s.Reverse().SelectMany((c, i) => ((c - '0') << (i & 1)).ToString()).Sum(c => (c - '0')) % 10 == 0;
}
Similarly for Scala, shouldn't it be:
def isLuhnValid(s : String) = 
    s.reverse.zipWithIndex.map({ case (c, i) => ((c - '0') << (i & 1))}).mkString.map(_ - '0').sum % 10 == 0
Rehno Lindeque (talk) 09:33, 1 June 2011 (UTC)
thread_title:  Errors in the examples 

message: I added a haskell example that elegantly describes the algorithm from a high level as "reverse the string", "convert to digits", "double every second element", "convert to a string", "convert to digits", "sum them all up".
Rehno Lindeque (talk) 11:01, 1 June 2011 (UTC)
thread_title:  Haskell example 

message: If anyone is interested, I've cleaned up the python code a bit to make it easier to understand for newer programmers. I used a less "clever" implementation, and tried to name variables more clearly. I also consolidated the checksum code, so it is implemented once, and used in two ways to check validity, and to find the last digit.
I've also put up a gist on github with the code and tests showing correctness. https://gist.github.com/1773914
def luhn_checksum(card_number):
    def digits_of(n):
        return [int(d) for d in str(n)]
    digits = digits_of(card_number)
    odd_digits = digits[-1::-2]
    even_digits = digits[-2::-2]
    checksum = 0
    checksum += sum(odd_digits)
    for d in even_digits:
        checksum += sum(digits_of(d*2))
    return checksum % 10
def is_luhn_valid(card_number):
    return luhn_checksum(card_number) == 0
def calculate_luhn(partial_card_number):
    return 10 - luhn_checksum(int(partial_card_number) * 10)
206.169.213.106 (talk) 21:08, 8 February 2012 (UTC)
replies: text: It looks good. I've put this version up in the article. – Acdx (talk) 11:55, 18 February 2012 (UTC)
replies: text:  The Python example doesn't clarify anything.  I have done minor programming with BuildBot; which I believe uses Python.  I have used Perl, lisp, Basic, C, Java, C++, 8 different assembler languages, JavaScript, Pascal and Fortran.  Any programmer who can not look at the table at the start and not translate it to the language they are using should not be programming.  This is not a statement against Phyton.  It is for the title of this section; remove all programming languages.  I agree with this. 71.19.161.130 (talk) 20:51, 5 November 2014 (UTC)
replies: text:  Agreed. American Express cards, in particular, use zero padding to adjust the position of the numbers. More concerning to me, however, is that this example is uncited and looks more like original research until you read this discussion, which no one should have to do. Then again, the code example was changed without a source, and thus... Nulbyte (talk) 21:00, 8 July 2015 (UTC)
thread_title:  Python Example 

message: Here is a basic Ruby solution to luhn10 validation.  I decided to place it here instead of the article.
def luhn10?(number)
    checksum = 0
    digits = split_integer(number).reverse
    digits.each_with_index do |d,i|
      checksum += i % 2 == 1 ? sum_digits(d*2) : d
    end
    (checksum % 10) == 0
  end
  def split_integer(number)
    digits = []
    number.to_s.split().each do |digit|
      digits << digit.to_i
    end
    digits
  end
  def sum_digits(number)
    split_integer(number).inject(0){ |sum,digit| sum += digit.to_i} 
  end
— Preceding unsigned comment added by  (talk • contribs) 
Bug fixes. Boud (talk) 21:23, 24 February 2015 (UTC)
thread_title:  Ruby Solution 

message: 1.From the left most digit, moving right, double the value of every second digit
This is incorrect; it works with odd-length numbers, like the example, but not even-length, like most credit cards.
Reading the edits, it looks like it was originally written differently (from the rightmost digit, moving left...), but the confusion is in what is being described- creating a check digit for a number or validating a number with a check digit.  The current description is creating a check digit for a number, so "from the rightmost digit, moving left..." is also incorrect, since the original number does not have the check digit.  "From the rightmost digit, moving left..." is correct for validating a number with a check digit.  — Preceding unsigned comment added by 64.193.143.107 (talk) 19:01, 17 April 2013 (UTC)
thread_title:  description incorrect 4/17/2013 

message: I'm not going into details, but there's simply too much wrong with the C implementation. The link needs to be removed or the author needs to fix this.
--eNTi (talk) 20:57, 13 May 2015 (UTC)
thread_title:  C example bogus 

message: Luhnc←{10|-+/,0 10⊤⍵×⌽(≢⍵)⍴2 1}  ⍝ calculate the check digit
Luhnv←{(⊃⌽⍵)≡Luhnc ¯1↓⍵}         ⍝ verify the check digit
      x← 7 9 9 2 7 3 9 8 7 1
      Luhnc x
3
      Luhnv x,3
1
A team effort of the "Array Oriented Functional Programming with Dyalog" workshop at FunctionalConf '16.   Roger Hui (talk) 10:41, 16 October 2016 (UTC)
thread_title:  APL Implementation 

message: I am very confused. Why do some of the implementations completely ignore the check digit and others don't? And why would the digit be ignored?  — Preceding unsigned comment added by Sollyucko (talk • contribs) 18:29, 4 April 2019 (UTC)
thread_title:  Confusion about check digit 

message: Twice, the "Description" mentions a "third row". I have no idea where that third row is and what it contains. What are rows one&two?  — Preceding unsigned comment added by 146.60.210.112 (talk) 14:40, 21 July 2019 (UTC)
thread_title:  "third row": what&where is it? 

message: I’ve no idea what this is supposed to be doing.  It looks incomplete as well as incorrect. Nick Levine (talk) 08:29, 11 May 2020 (UTC)
thread_title:  Pseudocode 

message: The sentence "The check digit is calculated by 10 - (s mod 10)." is not correct. If 's' is a multiple of 10 then the formula result is 10, which is not a digit. To correct this, the sentence should be updated to say "The check digit is calculated by (10 - (s mod 10)) mod 10.".  — Preceding unsigned comment added by 64.231.116.243 (talk) 02:13, 20 June 2022 (UTC)
thread_title:  Formula error 

message: Three comments on the pseudocode:
 int parity := (nDigits-2)modulus 2
First, there is no such operation as "modulus". The operation is called "modulo" or "mod" for short. The "modulus" is the second operand in a mod operation (in this case, 2) not the operation or the result of the operation. Second, unless card numbers can have no digits other than a check digit, "(nDigits-2) mod 2" is equal to "nDigits mod 2". Third, type declarations and casts are undesirable in pseudocode unless the meaning of the code is unclear without them. Zerotalk 05:58, 7 October 2022 (UTC)
thread_title:  pseudocode 

message: Why do all these implementations do the unnecessary steps of separating the check digit from the payload, subtracting the weighted payload sum from 10 and comparing it with the check digit?
All you have to do is leave the check digit as part of the payload, weight that position as 1, mod the the whole sum by 10 and compare it to 0. Tzadik (talk) 12:13, 4 January 2024 (UTC)
thread_title:  Unnecessarily complex implementations