in reply to Faster Luhn Check Digit Calculation?
I agree Inline::C will be fastest, but lookup tables can help a pure-Perl implementation a lot.
LUHN.pm:
package LUHN; use warnings; use strict; use List::Util qw(sum); use Data::Dump qw(dd); sub cd_kschwab { # pm#1226545 use integer; my $total = 0; my $flip = 1; foreach my $c (reverse split //, shift) { $c *= 2 unless $flip = !$flip; while ($c) { $total += $c % 10; $c = $c / 10; } } return (10 - $total % 10) % 10; } my @x2_cast_out_9 = ( # 0 1 2 3 4 5 6 7 8 9 # input number n 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 # output: n * 2, cast out 9 ); my @check_digit = map { (10 - $_ % 10) % 10 } 0 .. 9 * 15; sub cd_AnomalousMonk { my $ccn = shift; my $total = 0; for (1, 3, 5, 7, 9, 11, 13) { $total += substr($ccn, $_, 1); } for (0, 2, 4, 6, 8, 10, 12, 14) { $total += $x2_cast_out_9[ substr($ccn, $_, 1) ]; } return $check_digit[ $total ]; } sub cd_BrowserUk { # pm#1226582 use integer; my $s = $_[ 0 ]; my $total = 0; for my $i ( 0 .. 14 ) { my $d = substr( $s, $i, 1 ); unless( $i & 1 ) { $d *= 2; $d -= 9 if $d > 9; } $total += $d; } $total *= 9; return chop $total; } 1;
test_LUHN_1.pl:
# test_LUHN_1.pl 01dec18waw use warnings; use strict; use Benchmark qw(cmpthese); use LUHN; for ('000' .. '999') { my $ccn = '401135000000' . $_; my $kschwab_cd = LUHN::cd_kschwab ($ccn); my $AnomalousMonk_cd = LUHN::cd_AnomalousMonk ($ccn); die "$ccn: kschwab $kschwab_cd != $AnomalousMonk_cd" if $kschwab_cd != $AnomalousMonk_cd; } print "tested cc number range kschwab vs. AnomalousMonk ok \n"; for ('000' .. '999') { my $ccn = '401135000000' . $_; my $kschwab_cd = LUHN::cd_kschwab ($ccn); my $BrowserUk_cd = LUHN::cd_BrowserUk ($ccn); die "$ccn: kschwab $kschwab_cd != $BrowserUk_cd" if $kschwab_cd != $BrowserUk_cd; } print "tested cc number range kschwab vs. BrowserUk ok \n"; cmpthese (-1, { cd_kschwab => sub { LUHN::cd_kschwab ('401135000000000') }, cd_AM => sub { LUHN::cd_AnomalousMonk ('401135000000000') }, cd_BrowserUk => sub { LUHN::cd_BrowserUk ('401135000000000') }, });
Output:
c:\@Work\Perl\monks\kschwab>perl test_LUHN_1.pl tested cc number range kschwab vs. AnomalousMonk ok tested cc number range kschwab vs. BrowserUk ok Rate cd_kschwab cd_BrowserUk cd_AM cd_kschwab 52764/s -- -40% -62% cd_BrowserUk 88033/s 67% -- -37% cd_AM 139999/s 165% 59% --
Update: My solution and some of the others are hard-wired for generating a checksum for a 15-digit string. Here's a generalized variation. Caution: this is not well tested. Also, I don't know that I like the @check_digit look-up array all that much; it might chew up a lot of space in certain circumstances. BrowserUk's $total *= 9; return chop $total; eats less space and is only very slightly slower than the lookup.
sub cd_AM_HOP { # iterator solution my ($n_digits, # n of digits for which to generate luhn checksum ) = @_; my $order = $n_digits & 1; my @straight = grep +($_ & $order), 0 .. $n_digits-1; # dd '---', + \@straight; my @adjusted = grep !($_ & $order), 0 .. $n_digits-1; # dd '===', + \@adjusted; my @check_digit = map { (10 - $_ % 10) % 10 } 0 .. 9 * $n_digits; return sub { my $ccn = shift; # digits for which to generate checksum my $total = 0; for (@straight) { $total += substr($ccn, $_, 1); } for (@adjusted) { $total += $x2_cast_out_9[ substr($ccn, $_, 1) ]; } # return $check_digit[ $total ]; # return check digit $total *= 9; return chop $total; # return check digit }; # end sub iterator }
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Faster Luhn Check Digit Calculation?
by kschwab (Vicar) on Dec 01, 2018 at 16:20 UTC | |
by AnomalousMonk (Archbishop) on Dec 02, 2018 at 02:25 UTC | |
|
Re^2: Faster Luhn Check Digit Calculation?
by LanX (Saint) on Dec 01, 2018 at 21:40 UTC | |
by AnomalousMonk (Archbishop) on Dec 02, 2018 at 01:36 UTC | |
by LanX (Saint) on Dec 02, 2018 at 02:01 UTC | |
by AnomalousMonk (Archbishop) on Dec 02, 2018 at 02:47 UTC | |
by LanX (Saint) on Dec 02, 2018 at 02:59 UTC | |
by LanX (Saint) on Dec 02, 2018 at 03:09 UTC |