according to Luhn's algorithm, 5105105105105100 is invalid as the total comes out to be 25, but the check50 cs50 says its valid. and in places, they starting to double the digits from the first digit itself like in this case and some places from 2 digit like 378282246310005 in this.

am i understanding something wrong??


If you do the math it should equal 20, which would validate as the last number in the integer is 0. Make sure when a digit is doubled for the first calculation of Luhn's algorithm, you are taking sum of digits if it's double digits rather than the product itself. i.e (7*2 = 14) you would add 1 + 4, which would give you 5 rather than just using 14.

  • if i start fro 5 then it comes 20 and if i start from 1 it comes 25, and in the cs50 site they starting from the second digit i.e. 1 so then according to that it should be 25 Jun 14 '18 at 20:16
  • You can't start from the beginning of the cc number because your formula will be broken depending on whether there is an odd or even number of digits. The rule of thumb is that you start from the SECOND TO LAST digit and go backwards doubling every other number then add the rest of the numbers (starting with the LAST number). Therefore, whether the cc number is 13 or 16 digits, the rule won't change.
    – Jay
    Jun 14 '18 at 20:24
  • then according to that we get (1*2)+(5*2)+(0*2)+(1*2)+(5*2)+(0*2)+(1*2)+(0*2)+5+0+1+5+0+1+5+0=2+10+0+2+10+0+2+0+5+0+1+5+0+1+5+0=2+1+0+0+2+1+0+2+0+5+0+1+5+0+1+5+0=25 so card not valid Jun 14 '18 at 20:35
  • You are still doubling the wrong numbers. Start with the second to LAST digit (0, then 5, then 1, etc...). You are doubling with the second digit from the front which is incorrect.
    – Jay
    Jun 14 '18 at 20:50

Not the answer you're looking for? Browse other questions tagged .