luhn's algorithm

TomBascom

Curmudgeon
Rather than just feeding you the code maybe it would be better (and you would learn more) if you show us what you have tried and ask questions about the areas that are giving you trouble.
 
I actually wanted to find an approach about it. I was thinking about breaking the number into a temp table and then create my logic around it. Should this be a good approach or is there anything which can be done more smartly ?
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
I think that's overkill. All you need is to turn strings to integers, and the rest is arithmetic.

First work it out on paper. Then decide on your algorithm. One DO loop is all you need. And remember that not all PANs are the same length.
 

TomBascom

Curmudgeon
I agree with Rob. A TT seems a bit over the top. A few variables should be more than adequate. I would also encapsulate the routine in a procedure or function to make it easy to re-use.
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
Unless this is an academic exercise, another thing to bear in mind is that these days a lot of issuers and processors don't even do mod-10 checks anymore. If your volumes are high, these additional checks just add to your transaction processing time and make timeouts incrementally more likely; CPU cycles aren't free.

If you're an issuer, you know which cards are yours because you already have the full PAN in your card table. Invalid ones (those that look legit but have bad check digits) just won't be there. And if you're a processor, you probably just care about the BIN, for routing transactions to the issuer, and not the rest of the PAN.

Calculating check digits is still used however in provisioning cards, when you create the PAN in the first place.
 
I am sorry i should be more specific about my requirement. Actually we are planning to use luhn's algorithm to generate the check digit on kanban cards number in our ERP system. We are not doing validation on credit card number. I guess mod 10 check is more than enough to avoid errors on doing transactions on kanban card.
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
I suppose so. Luhn is used on all kinds of cards: debit/credit cards, loyalty, gift, library, health, etc. So it should suit your needs as well.
 
Top