Content deleted Content added
Undid revision 1289018748 by 148.252.146.68 (talk) |
|||
(28 intermediate revisions by 22 users not shown) | |||
Line 1:
{{WikiProject banner shell|class=C|
{{WikiProject
{{WikiProject Computing|auto=inherit}}
}}
== Worked Example ==
Line 12 ⟶ 13:
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.
ممكن اعرف انت في حياتي [[User:Maommad|Maommad]] ([[User talk:Maommad|talk]]) 11:51, 6 January 2021 (UTC)
== No longer valid? ==
Line 59 ⟶ 62:
:::You are correct. According to Vital (Now TSYS) the wording is correct and the pseudo-code is wrong. I have recommended that it be changed. Have to be careful though in how you word the for loop cause not all languages implement for the same. While loops may be safer and more language independant. --[[User:dmprantz|dmprantz]] 17 October 2006 (UTC)
Interesting, the pseudocode matches the description of the algorithm now, but the example given is wrong (it doubles the OTHER digits, starting with the first from the end of the payload, and not with the second, while the description clearly says "every second" - it should say "every second counting the whole number, not only the payload"). More interesting, running the given number on 6 different online calculators, 4 of them gave the result as in the example (i.e. 4) but the other 2 gave the result as in the description and pseudocode (i.e. 3) :D My cards numbers all match the example, and not the description/pseudocode. Something is fishy in this article :P ... [[User:LaurV|LaurV]] ([[User talk:LaurV|talk]]) 04:30, 8 March 2025 (UTC)
=== Computing the check digit ===
Line 158 ⟶ 162:
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;
}
</syntaxhighlight>
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
</syntaxhighlight>
[[User:Rehno Lindeque|Rehno Lindeque]] ([[User talk:Rehno Lindeque|talk]]) 09:33, 1 June 2011 (UTC)
Line 204 ⟶ 208:
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):
Line 222 ⟶ 226:
def calculate_luhn(partial_card_number):
return 10 - luhn_checksum(int(partial_card_number) * 10)
</syntaxhighlight>
[[Special:Contributions/206.169.213.106|206.169.213.106]] ([[User talk:206.169.213.106|talk]]) 21:08, 8 February 2012 (UTC)
:It looks good. I've put this version up in the article. – [[User:Acdx|Acdx]] <small>(<i>[[User_talk:Acdx|talk]]</i>)</small> 11:55, 18 February 2012 (UTC)
Line 237 ⟶ 241:
Here is a basic Ruby solution to luhn10 validation. I decided to place it here instead of the article.
<
def luhn10?(number)
checksum = 0
Line 256 ⟶ 260:
split_integer(number).inject(0){ |sum,digit| sum += digit.to_i}
end
</syntaxhighlight>
<small><span class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:{{{1}}}|{{{1}}}]] ([[User talk:{{{1}}}|talk]] • [[Special:Contributions/{{{1}}}|contribs]]) </span></small><!-- Template:Unsigned -->
Line 291 ⟶ 295:
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? <!-- Template:Unsigned --><small class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:Sollyucko|Sollyucko]] ([[User talk:Sollyucko#top|talk]] • [[Special:Contributions/Sollyucko|contribs]]) 18:29, 4 April 2019 (UTC)</small> <!--Autosigned by SineBot-->
== "third row": what&where is it? ==
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? <!-- Template:Unsigned IP --><small class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/146.60.210.112|146.60.210.112]] ([[User talk:146.60.210.112#top|talk]]) 14:40, 21 July 2019 (UTC)</small> <!--Autosigned by SineBot-->
I noticed the row mystery. Overall, this article is the worst description of anything I ever saw. Absolutely useless.
== Pseudocode ==
I’ve no idea what this is supposed to be doing. It looks incomplete as well as incorrect. [[User:Nick Levine|Nick Levine]] ([[User talk:Nick Levine|talk]]) 08:29, 11 May 2020 (UTC)
== Formula error ==
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.". <!-- Template:Unsigned IP --><small class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/64.231.116.243|64.231.116.243]] ([[User talk:64.231.116.243#top|talk]]) 02:13, 20 June 2022 (UTC)</small> <!--Autosigned by SineBot-->
== pseudocode ==
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. [[User:Zero0000|Zero]]<sup><small>[[User_talk:Zero0000|talk]]</small></sup> 05:58, 7 October 2022 (UTC)
== One alternative formula is wrong ==
The formula 10[s/10] -s is wrong as written. The use of integer math is implied, for if fractional values are allowed, the formula is zero for all s. With internet arithmetic and given example where’s=67, the formula gives -7, not the correct answer of 3.
I would suggest it be r)e-written as
10 + { INT(10[s/10])- s}
Or perhaps better
10-{ s - INT( 10[s/10])}
This Alternative formula gives 3, but I have not carefully checked that it agrees with Luhn’s original formula.
Alternatively, just delete that line in the article. [[User:GWT45|GWT45]] ([[User talk:GWT45|talk]]) 04:10, 6 February 2023 (UTC)
:I think you've misread the formula: it's using the "ceiling" operator rather than square brackets. If s=67, then <math>\lceil s/10\rceil = \lceil 6.7\rceil = 7</math>. So the full equation evaluates to 3.
:The first equation <math>10 - (s\operatorname{mod} 10)</math> does look wrong though, since it'd result in 10 rather than 0 if s is a multiple of 10. [[User:JamesHenstridge|James]] ([[User talk:JamesHenstridge|talk]]) 10:54, 4 July 2023 (UTC)
== Unnecessarily complex implementations ==
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. [[User:Tzadikv|Tzadik]] ([[User talk:Tzadikv|talk]]) 12:13, 4 January 2024 (UTC)
== the C# example ==
The code is not correct, nor does it bring a correct result. [[User:לומד|לומד]] ([[User talk:לומד|talk]]) 08:26, 21 February 2024 (UTC)
:How is it wrong? Can you explain what the problem is, or give an example of an input that causes it to have the wrong output?
:[[User:Sollyucko|Solomon Ucko]] ([[User talk:Sollyucko|talk]]) 14:44, 21 February 2024 (UTC)
::- `in int[1] digits` doesn't compile because of the `1` (unless this is an upcoming feature of the language, but even so we probably don't want an array with only one element)
::- whether it "doubles" a digit is based on its index from the start, not the end, of the input
::- if the check digit needs to be 0, the code instead checks that it is 10
::There used to be a working example, but all the examples were deleted (see "Remove all implementation examples" above) and someone has since added this one instead. [[User:Rawling|Rawling]] ([[User talk:Rawling|talk]]) 10:09, 9 March 2024 (UTC)
== Is the final step in calculating the checksum actually correct? ==
According to the article, the last step is "The check digit is calculated by <math>(10 - (s \bmod 10))</math>". Unfortunately, following this logic, it would be IMPOSSIBLE for the check digit to ever equal 0. The value returned by <math>(s \bmod 10)</math> can only range between 0 and 9. So when you subtract that number from 10, you get a value that ranges between 10 and 1. This set of numbers is completely missing a 0, and it also includes a 10 which is too large for a single digit. The article here doesn't mention how this is to be fixed. I would assume if the output is 10 then it would then just be converted to 0 in an additional final step, but that's not mentioned anywhere in the article, so I'm not sure how that should actually be handled. [[Special:Contributions/73.169.149.34|73.169.149.34]] ([[User talk:73.169.149.34|talk]]) 20:16, 27 September 2024 (UTC)
|