, , , , , , , , ,

Hans Peter LuhnSome people ask me about how banks or online retailer know when their credit card number is wrong without a regular request or how their mobile services provider or mobile apps know their smartphone or tablet identification(IMEI) are invalid due to initial system configuration manipulation.

What if I tell you the similarity between this identifications and the java code behind the scene?

It sounds really cool, yeah?

Hans Peter Luhn (July 1, 1896 – August 19, 1964) was a computer scientist for IBM, and creator of the Luhn algorithm and KWIC (Key Words In Context) indexing. He was awarded over 80 patents.

Luhn was born in Barmen, Germany (now part of Wuppertal) on July 1, 1896. After he completed secondary school, Luhn moved to Switzerland to learn the printing trade so he could join the family business. His career in printing was halted by his service as a communications officer in the German Army during World War I. After the war, Luhn entered the textile field, which eventually led him move to the United States, where he invented the Lunometer, a thread counting gauge still on the market. From the late twenties to the early forties, during which time he obtained patents for a broad range of inventions, Luhn worked in textiles and as an independent engineering consultant. He was hired by IBM as a senior research engineer in 1941, and soon became manager of the information retrieval research division.
His introduction to the field of documentation/information science came in 1947 when he was asked to work on a problem brought to IBM by James Perry and Malcolm Dyson that involved searching for chemical compounds recorded in coded form. He came up with a solution for that and other problems using punched cards, but often had to overcome the limitations of the available machines by coming up with new ways of using them. By the dawn of the computer age in the 1950s, software became the means to surmount the limitations inherent in the punched card machines of the past.
Luhn spent greater and greater amounts of time on the problems of information retrieval and storage faced by libraries and documentation centers, and pioneered the use of data processing equipment in resolving these problems. “Luhn was the first, or among the first, to work out many of the basic techniques now commonplace in information science.” These techniques included full-text processing; hash codes; Key Word in Context indexing (see also Herbert Marvin Ohlman); auto-indexing; automatic abstracting and the concept of selective dissemination of information (SDI).
Two of Luhn’s greatest achievements are the idea for an SDI system and the KWIC method of indexing. Today’s SDI systems owe a great deal to a 1958 paper by Luhn, “A Business Intelligence System”, which described an “automatic method to provide current awareness services to scientists and engineers” who needed help to cope with the rapid post-war growth of scientific and technical literature. Luhn apparently coined the term business intelligence in that paper.  –Wikipedia


Luhn was great obviously, his work in our days is useful to validate CreditCard Luhnlarge numbers like credit card and International Mobile System Equipment Identity (IMEI) in every smartphone or tablet over the planet and you can see his legacy in all rightmost digit of those items.


The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This number must pass the following test:


  1. From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then sum the digits of the products (e.g., 12: 1 + 2 = 3, 16: 1 + 6 = 7 ).
  2. Take the sum of all the digits.
  3. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

Assume an example of an account number “123456789” that will have a check digit added, making it of the form 123456789X:
Account number: 1 2 3 4 5 6 7 8 9
Double every other: 26 4 10 6 14 8 18
Sum digits: 2 2 6 4 1 6 5 8 43

  1. Compute the sum of the digits (43).
  2. Take the units digit (3).
  3. Subtract the units digit from 10 (10-3). Thus, x=7.
  4. Result: 1234567897

Alternative method:

  1. The sum of all the digits in the third row is (43).
  2. Compute the sum by 9 times that value modulo 10 (43 × 9 = 387).
  3. The last digit, 7, is the check digit. Thus, x=7.
  4. Result: 1234567897

One Java implementation to validate a number and generate Luhn digit appended to source number might be:

package jorgeCode.Lunh;
public class Luhm {
  public static boolean isValidNumber(String s) {
   return doLuhn(s, false) % 10 == 0;

  public static String generateDigit(Object s) {
   return (generateDigit((String) s));

  public static String generateDigit(String s) {
    int digit = 10 - doLuhn(s, true) % 10;
    return (s + digit);

  private static int doLuhn(String s, boolean evenPosition) {
    int sum = 0;
    for (int i = s.length() - 1; i >= 0; i--) {
      int n = Integer.parseInt(s.substring(i, i + 1));
      if (evenPosition) {
        n *= 2;
        if (n > 9) {
          n = (n % 10) + 1;
      sum += n;
      evenPosition = !evenPosition;
    return sum;

Jorge Fernandes is Bachelor in Computer Science experienced in telecommunications, security, banking and programmer since 1984. He works in Valhala Networks focused on high-level decisions about scientific and technological policies and strategies of projects oriented to develop web and standalone applications, as well as smart solutions at mobiles. He always oriented to produce innovator products.