Trying to see if there's a faster way to generate the LUHN check digit that goes at the end of a credit card number. At the moment, I'm using this, cribbed from Algorithm::LUHN, only change was removing some not-needed input verification.
my %map = map { $_ => $_ } 0..9;
sub check_digit {
my @buf = reverse split //, shift;
my $totalVal = 0;
my $flip = 1;
foreach my $c (@buf) {
my $posVal = $map{$c};
$posVal *= 2 unless $flip = !$flip;
while ($posVal) {
$totalVal += $posVal % 10;
$posVal = int($posVal / 10);
}
}
return (10 - $totalVal % 10) % 10;
}
# example use
for (401135000000000..401135000000099) {
my $cd=check_digit($_);
print "$_$cd\n";
}
You give it the first 15 digits of a credit card number, and it calculates the 16th digit. It's reasonably fast now, calculating about 135,000 cc numbers per second (older i7), but would like to see if there's anything that might speed it up, other than running more than one instance (already doing that, but trying to focus on just the single core speed here).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.