Sha256: 27a5aa660190f3b80a607dbdeb42e06458abceed68edc8a01d69254fd5e4988e

Contents?: true

Size: 527 Bytes

Versions: 48

Compression:

Stored size: 527 Bytes

Contents

package Luhn 3;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT_OK = qw(is_luhn_valid);

sub is_luhn_valid {
  my ($input) = @_;
  $input =~ s/\s+//g;
  if (length $input < 2 || $input =~ /\D+/) {return 0}

  my @num = split(//, $input);
  unshift @num, 0 if scalar @num % 2;

  my $count = 1;
  my $sum = 0;

  foreach (@num) {
    if ($count % 2) {
      $sum += $_ * 2;
      $sum -= 9 if $_ * 2 > 9;
    }
    else {
      $sum += $_;
    }
    $count += 1;
  }

  if ($sum % 10) {return 0};
  return 1;
}

1;

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.158 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.157 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.156 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.155 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.154 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.153 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.152 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.151 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.150 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.149 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.148 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.147 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.146 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.145 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.144 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.143 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.142 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.141 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm
trackler-2.2.1.140 tracks/perl5/exercises/luhn/.meta/solutions/Luhn.pm