Wherein we find ourselves yet again enmeshed in numerological traps, searching for an order, a commonality, a sensibility to escape into, from an essentially unordered universe of churning chaos…

#### THE WEEKLY CHALLENGE – PERL & RAKU #162 Task 1

**“There were fourteen steps exactly fourteen. But the top one was smaller, out of proportion, as if it had been added to avoid the evil number.”**

**— Stephen King, Salem’s Lot**

### ISBN-13

**Submitted by:** Mohammad S Anwar

Write a script to generate the check digit of given ISBN-13 code. Please refer wikipedia for more information.

##### Example

`ISBN-13 check digit for '978-0-306-40615-7' is 7.`

#### First a little Commentary…

Since our recent forays into dark amber theory, I’ve become overly suspicious whenever the pernicious and odious number 13 mysteriously appears. It has become abundantly clear to me that there are forces out there that view evil as a mere extension of the natural order of the mathematical world behind our world. I do not like it.

Frankly, the whole thing makes me more than a bit uncomfortable.

One might argue that sometimes, though, the number thirteen is just the number thirteen. It’s a happy number, you say. A lucky number even!

As for that last statement, I will note that I remain skeptical as 13 is most obviously odd.

All this goes to show that number theory is a deep and complex study in itself without bringing ethics into it. The numbers themselves are aloof — they don’t care what we think of them and never have. We should have left well enough alone, without resorting to naming magic.

**Judgement is for cowards afraid of the unknown. **

I say let the numbers be.

#### ANALYSIS

But we were talking about books, right?

The International Standard Book Number, ISBN, is labeling systems designed to assign every book title published a unique identifier, with separate assignments for alternate or updated editions and formats. Reprints of a generally-unaltered publication run retain the same label until substantial changes warrant the reclassification as a new edition.

The modern version of this label, the ISBN-13, is a 13-digit classification that when parsed descends through a series of increasingly-specific codes; from broad language groups to areas, publishers within that area and lastly serial block assignments for publishers to label individual works. The actual assignment of digit positions among the sections is not absolutely fixed, so parsing can be complicated. The largest publishers, for example, are assigned short numeric codes, allowing more digits positions left over for their larger block assignments. This is of course a simplification but gets across the idea. In any case the final number is fixed to 13 total digits in the most modern manifestation of the scheme, the first 12 of which are the taxonomic outline.

The thirteenth position is a check digit is calculated from the others through a well-defined formula, with the intent to verify that no step within the multi-stage process was mishandled, producing an incorrect ISBN.

Well, that’s the way it’s supposed to work, at least. “You can lead a horse to water” and all that still applies, as the final assignment of a number to a specific number to a title is done not by the issuing agency but rather the publishing house.

In fact incorrect ISBNs do make it into publications, or are omitted entirely. ISBNs are more commonly incorrectly assigned across differing editions and sometimes even accidentally reused. Such are the perils of having publishers managing the final assignment of these numeric labels. When anyone with a laser printer can in theory become an imprint, we can expect to end up with a whole statistical gamut of competency to show for it. But the system really does mostly work, and as the penalty for fouling one’s ISBN codes is an inability for large computerized distribution networks to order and sell your product this does provide a strong motivator to get the details right.

#### METHOD

The ISBN-13 checksum digit is calculated by multiplying each digit in the numeric code by alternating coeficients of 1 and 3 and then summing them. The result is divided out modulo 10 and the checksum is that digit that when added to the result brings the total to 0.

Or in formulaic notation, with the digit positions as a indexed array of values, we get:

**a**_{0} + 3**a**_{1} + **a**_{2} + 3**a**_{3} + **a**_{4} + 3**a**_{5} + **a**_{6} + 3**a**_{7} + **a**_{8} + 3**a**_{9} + **a**_{10} + 3**a**_{11} + **a**_{12} ≡ 0 *mod *10

##### PERL 5 SOLUTION

The most complicated part of this solution is creating a flip-flop coefficient that alternates between the values of 1 and 3, acting as a multiplier at each position. The digits are matched out and captured by a regular expression, resulting in an array of values, and the checksum derived by applying the formula expansion and subtracting the modulo result from 10 and correcting 10 to 0.

```
use warnings;
use strict;
use utf8;
use feature ":5.26";
use feature qw(signatures);
no warnings 'experimental::signatures';
my @tests = ( '978-0-306-40615-7', ## Error-Correction Coding
## for Digital Communications
'978-0-547-54927-9', ## The Exegesis of Philip K. Dick
'9780416469608' ## Tao of Pooh
);
for my $isbn (@tests) {
my @arr = $isbn =~ /(\d)/g;
my $c = 1;
my $sum = 0;
$sum += $_ * (++$c * 2 % 4 + 1) for @arr[0..11];
my $ck = 10 - $sum % 10;
$ck == 10 and $ck = 0; ## correct 10 -> 0
say <<~"END";
input $isbn
checksum $ck
END
}
```

*The Perl Weekly Challenge, that idyllic glade wherein we stumble upon the holes for these sweet descents, is now known as *

**The Weekly Challenge – Perl and Raku**

*It is the creation of the lovely Mohammad Sajid Anwar and a veritable swarm of contributors from all over the world, who gather, as might be expected, weekly online to solve puzzles. Everyone is encouraged to visit, learn and contribute at *