Task

Validate the check digit of an ISBN-13 code:

   Multiply every other digit by  3.
   Add these numbers and the other digits.
   Take the remainder of this number after division by  10.
   If it is  0,   the ISBN-13 check digit is correct.


Use the following codes for testing:

   978-1734314502       (good)
   978-1734314509         (bad)
   978-1788399081       (good)
   978-1788399083         (bad)


Show output here, on this page



See also

   for details:   13-digit ISBN method of validation.       (installs cookies.)


F is_isbn13(=n)
   n = n.replace(‘-’, ‘’).replace(‘ ’, ‘’)
   I n.len != 13
      R 0B
   V product = sum(n[(0..).step(2)].map(ch -> Int(ch)))
             + sum(n[(1..).step(2)].map(ch -> Int(ch) * 3))
   R product % 10 == 0
 
V tests = |‘978-1734314502
            978-1734314509
            978-1788399081
            978-1788399083’.split("\n")
 
L(t) tests
   print(‘ISBN13 ’t‘ validates ’is_isbn13(t))