1

I am trying to implement Luhn's Algorithm for validating credit cards, there is a step where we reverse the card number, why is it necessary to reverse the number?

Ambitions
  • 2,369
  • 3
  • 13
  • 24
  • There is no such step in the [Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm). – AlexP Apr 15 '17 at 17:09
  • @AlexP Some websites are writing this step. I will follow what Wikipedia says. – Ambitions Apr 15 '17 at 17:15
  • 1
    The confusion is probably in part because you should start with the rightmost digit, then move left. This is of course the same as reversing the digits, then moving right. – PaulG Apr 18 '17 at 11:59

1 Answers1

2

The Luhn algorithm involves doubling every other digit in the number, such that the rightmost digit (the check digit) is NOT doubled. This means that the leftmost digit may or may not be doubled, depending on whether the number has an odd or even quantity of digits. One way of dealing with this variation would be to reverse the number, and always start with a non-doubled digit - but that seems like overkill to me; there are various solutions (such as indexing through the digits in reverse order) that are less work than reversing a number.

jasonharper
  • 9,450
  • 2
  • 18
  • 42