سؤال

I have the following constraints for a number that will be recognized from an image:

  • 6 digits data
  • 1 digit error correction
  • The first 6 digits cannot be changed (they must be human readable)
  • The check digit must remain a numeral

The error correction scheme currently is based on a checksum, such that the 7th digit is the last digit of the sum of the first 6 digits.

E.g.

123456 => 1234561
999999 => 9999994
472912 => 4729125
219274 => 2192745

How can I determine the number and types of errors this scheme can detect/correct, and is there a scheme that will provide better error detection? (Error detection is more important than error correction for my use case).

هل كانت مفيدة؟

المحلول

You can try Luhn, it's a little more complex that what you describe but it will meet your requirements.

A copy paste from wikipedia:

The Luhn algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09 to 90 (or vice versa). It will detect 7 of the 10 possible twin errors (it will not detect 22 ↔ 55, 33 ↔ 66 or 44 ↔ 77).

Other, more complex check-digit algorithms (such as the Verhoeff algorithm and the Damm algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports non-numerical strings.

Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that pad to a specific number of digits (by converting 1234 to 0001234 for instance) can perform Luhn validation before or after the padding and achieve the same result.

Prepending a 0 to odd-length numbers enables you to process the number from left to right rather than right to left, doubling the odd-place digits.

The algorithm appeared in a US Patent for a hand-held, mechanical device for computing the checksum. It was therefore required to be rather simple. The device took the mod 10 sum by mechanical means. The substitution digits, that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top