0

I'm having trouble with the checksum in the #credit problem. I have no clue on how to operate specific digits within the credit card number (specifically, multiplying the digits in the odd positions by 2). Any ideas on how to approach this?

1 Answer 1

1

There are a few ways to approach this. Some people take the number as a string. With that, each digit character has its own byte. I went another way, using get_long. Applying %10 to the number returns the last decimal digit. Applying /10 returns the number without the last decimal digit (integer division truncates the result). Make sure to operate on a copy of the number if you still need it somewhere else.

The last digit is never doubled. The second to last always is, and from there it's every second digit, so starting at the end (as you would if processing the last digit at any time) makes things easier. You could have a loop and always process one digit, switching between the two modes. Or your loop could process two digits at a time, one with and one without the doubling.

1
  • Thank you so much! Nov 13, 2019 at 14:47

You must log in to answer this question.

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