fork download
  1. use List::Util qw(sum);
  2.  
  3. sub is_valid_isbn {
  4. my @digits = split //, shift;
  5. return ((@digits == 13) and ((sum map{ $digits[$_] * ($_ % 2 ? 3 : 1) } (0..12)) % 10 == 0));
  6. }
  7.  
  8. foreach("9784062772211", "9784150315684"){
  9. printf("%s => %s\n", $_, (is_valid_isbn($_) ? "OK" : "NG"));
  10. }
Success #stdin #stdout 0s 4888KB
stdin
Standard input is empty
stdout
9784062772211 => OK
9784150315684 => NG