Categories

Innerworkings

This site is powered by WP and hosted by 1&1. Made with valid XHTML. Version the Second.

Sonic Light Speed
Vector Green
OTP Manager
Maze Solver
29.05.08

The IBM Check

“To Err is Human…” so goes the old truism, but luckily computers can often help in error correction. As anyone who has typed a long sequence of numbers (such as a credit card number) knows, it is extremely easy to make a mistake while typing. Common errors include typing the wrong number, switching two numbers, forgetting a number, and inserting an extra number.

There are many different error detection methods, both for detecting errors in binary numbers and in decimals. One well-designed and widely used check method for decimal numbers is called the IBM Check.

The check is actually quite simple. Given an eight digit number, the number is valid if:


(f(d1, 2) + d2 + f(d3, 2) + d4 + f(d5, 2) + d6 + f(d7, 2) +  c) mod 10 = 0

where c is the check digit (d8, the last digit in the sequence) and f(n) is defined as:


public int f(m, n) {
           char[] tmp = Integer.toString(m * n).toCharArray();

           int val = 0;

           for (int i=0; i < tmp.length; i++) {
           	  val += Character.getNumericValue(tmp[i]);
           }

           return val;
}

So f(m, n) simply multiplies the two numbers and adds the digits of the product. To illustrate:


f(7, 2) --> 7*2 = 14 --> 1+4 = 5

So f(7, 2) returns 5. Now that the check process is clear, it is quite easy to generate a valid sequence. To generate an eight digit sequence, seven random digits would be plugged in the equation, leaving c as the only unknown. Simple algebra and logic is all that is then required to find c. And then voilĂ ! — an IBM check-valid number.

It should be noted that the IBM check has a few weaknesses. Like any other check process, it is possible that a user might enter in a wrong number that still satisfies the check. Furthermore, flipping a 9 and a 0 won’t be detected by the check because f(9, 2) = 9 and f(0, 2) = 0. The IBM check is still, however, a very useful tool and a very popular one. In fact, credit cards often use this check. Think of that next time you type those sixteen digits.

Leave a Comment