Login   Register  
PHP Classes
elePHPant
Icontem

A quicker way

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      PHP Luhn Algorithm  >  All threads  >  A quicker way  >  (Un) Subscribe thread alerts  
Subject:A quicker way
Summary:A quicker way
Messages:3
Author:H Johnson
Date:2014-02-09 22:08:56
Update:2014-02-10 17:52:54
 

  1. A quicker way   Reply  
Picture of H Johnson
H Johnson
2014-02-09 22:08:56
This will do the same thing, just more quickly:

var sumTable = [[0,1,2,3,4,5,6,7,8,9],
[0,2,4,6,8,1,3,5,7,9]];

var sum = 0, flip = 0;
for (var i=cardNo.length-1; i>= 0; i--)
sum += sumTable[flip++ & 0x1][Number(cardNo.charAt(i))];

Have fun with it. I rewrote it in php from what someone else had done in java.

  2. Re: A quicker way   Reply  
Picture of Rolands Kusins
Rolands Kusins
2014-02-10 08:22:17 - In reply to message 1 from H Johnson
Thank you! Made some performance improvements on my method and added yours. On my laptop, with 1 000 000 number check digit calculation there is approx 1 second improvement with algorithm you suggested.

  3. Re: A quicker way   Reply  
Picture of H Johnson
H Johnson
2014-02-10 17:52:54 - In reply to message 2 from Rolands Kusins
You're welcome.

I thought it as a pretty cool algorithm when I found it, and a good example of how one can think about a problem from a very different perspective. I like you started with a much more complicated approach.

Anyway, while I don't use it currently, because my payment provider has this checker built in to their stuff, I saved it as just one of those very neat things I had seen.