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;