Luhn Algorithm

Back to General discussions forum

Stefan Dell     2015-07-16 18:12:29

Can you explain how to recognize which digit swap should be made? My code is correct and i find the correct digits. Thanks

Rodion (admin)     2015-07-16 19:01:27
User avatar

Hi Stefan! I'm not sure I understand your trouble, but you can try to swap each pair starting from left and stop when you find which results in correct checksum.

Stefan Dell     2015-07-16 19:14:23
6858060553208574 2127900796513965 6732143959375747 
6858060553208574 2129700796513965 6372143959375747 

as you can see i find the correct number in '?' place, but in some numbers it swaps some digits. why this is happening in solution? what am i missing? thanks

Rodion (admin)     2015-07-17 06:50:38
User avatar

Could you post all three things, please:

  • input data;
  • your answer;
  • expected answer.

Of course feel free to cut unnecessary things out.

> but in some numbers it swaps some digits

I thought there are two type of "errors" in the input data:

  • missing digits;
  • swapped digits;

Each number will have only one type of error, of course - either ? or swap. So you need to replace question mark with digit, or find which digits to swap (trying every pair starting from the left).

Did this make sense?

Please login and solve 5 problems to be able to post at forum