fork download
  1. use strict;
  2. use warnings;
  3.  
  4. sub is_valid_isbn {
  5. my @digits = split //, shift;
  6.  
  7. (@digits == 13) or return;
  8.  
  9. my $n = 0;
  10. foreach(0..12){
  11. $n += $digits[$_] * ($_ % 2 ? 3 : 1);
  12. }
  13.  
  14. return ($n % 10 == 0);
  15. }
  16.  
  17. foreach(qw( 9784062772211 9784150315684 )){
  18. printf("%s => %s\n", $_, (is_valid_isbn($_) ? "OK" : "NG"));
  19. }
  20.  
Success #stdin #stdout 0s 4684KB
stdin
Standard input is empty
stdout
9784062772211 => OK
9784150315684 => NG