Solving seemingly simple math problems using Perl
Checking Is Better
The phrase "trust is good, checking is better" turns out to be true here. Listing 2 reads the formatted output from Listing 1 and, using the hash reference $met
, checks whether the participants actually are in a class with different people each time. If the script comes back without any output, everything is okay.
Listing 2
combiverify
Prime Powers are More Difficult
However, if the array dimension is not a prime number, as with the initially requested 9x9 squares, the matter is more difficult. A solution doesn't even exist for a 6x6 arrangement, which Euler found out when he tried to make 36 officers from six different regiments, each with six different service ranks, march for a parade without either ranks or regimental affiliations repeating within a row or a squad [6].
He correctly concluded that this was theoretically impossible, but he also carelessly and hastily got carried away, making the assumption that there would also be no solution for dimensions 4n+2. A huge mistake! More than 150 years later, precisely in 1959, it turned out that there are actually solutions for 10x10 and 14x14. However, the fact that the problem of 36 officers is unsolvable has been well and truly proved by now.
NineElement Galois Field
If the dimension is a prime power (e.g., 9, the second power of the prime number 3), then you can use a Galois field F to construct the squares. This is a collection of n numbers for which the arithmetic operations of addition (+) and multiplication (*) are defined and, in turn, return a result in F.
In relation to the problem with the nine teachers, 18 male students and 27 female students, the Canadian mathematician suggested dividing them into six groups, each with nine participants: the teachers, the A and Bmale students and the C, D, and Efemale students. He then formed the nineelement body made up of complex numbers (i2=1),
F = {0, 1, 2, i, 1+i, 2+i, 2i, 1+2i, 2+2i}
reducing the addition and multiplication with modulo 3, thus ensuring that a combination of two elements from F again results in an element from F. For example,
(1 + 2i) * (1 + i) = 1 + 3i
which modulo 3 reduces to 2+0i, so to 2, which is again in F.
If you characterize the operations + and * as the field operations of addition and multiplication with modulo 3 that keeps the result within F, you can generate the numbers of participants in the groups using the formulas in Figure 8. In this representation, the variable d refers to each day's sequence number (1 to 9) and g to the number of the group (also 1 to 9). The result is the sequence number of the individual participant from the six different subclasses showing who is in the particular group on the specified day.
Listing 3 merely implements these formulas and outputs the result for nine days, as initially shown in Figure 3. The array @field
defines the complex numbers in the Galois field in lines 5 to 9, and the function mapback()
from line 17 again selects the index of the element in F for a numeric result (e.g., the result of multiplication).
Listing 3
combidesign
The functions fadd()
and fmult()
(lines 41 and 52) implement the addition or multiplication reduced by modulo 3 of two elements from the field F. They receive the elements' index numbers, select the associated complex numbers from F, apply the respective operation to the real and imaginary parts of the operands, and trace the result back to the index of an element in F using mapback
.
The Galois field is named after the French mathematician Évariste Galois, who achieved a major mathematical breakthrough at the tender age of 18. What he might have achieved is pure speculation because he was involved in a pistol duel at the age of 20 in which he drew the short straw, dying the next day of his gunshot wound.
That's how it was in the 19th century: milestones in discrete mathematics one day and out with the flintlock pistol the next morning to shoot offenders. Life sure was exciting back then!
Mike Schilli
Michael Schilli works as a software engineer in the San Francisco Bay Area in California. In his column, which has been running since 1997, he researches practical applications of the Perl scripting language. He is happy to answer questions at mailto:mschilli@perlmeister.com.
Infos
 Usenet posting from 1996: https://groups.google.com/forum/m/?fromgroups#!topic/rec.puzzles/kSsEDkm1_gw
 Galois field: https://en.wikipedia.org/wiki/Finite_field
 Latin square: https://en.wikipedia.org/wiki/Latin_square
 "Mutually Orthognal Latin Squares and Finite Fields" by Padraic Bartlett, 2012: http://math.ucsb.edu/~padraic/mathcamp_2012/latin_squares/MC2012_LatinSquares_lecture2.pdf
 Listings for this article: ftp://ftp.linuxmagazine.com/pub/listings/magazine/179
 Thirtysix officers problem: http://en.wikipedia.org/wiki/Thirtysix_officers_problem
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News

VMware Rolls Out Essential PKS
Virtualization vendor brings commercial support to upstream Kubernetes.

Linux 5.0 Is Here
Linus says don't get excited, but the new release contains some significant updates.

Kali Linux 2019.1 Released
The favorite Linux distro of Mr. Robot gets the first update of 2019.

Linux Foundation Releases a New Draft of OpenChain Spec
OpenChain provides a standard for open source compliance throughout the software supply chain.

Linux Kernel Continues To Offer Mitigation for Spectre Mitigation
Kernel 4.19 has added another family of Spectre vulnerabilities to its list of mitigating the mitigation.

SpeakUp Trojan Targets Linux Servers
It’s exploiting a known vulnerability.

KDE Plasma 5.15 Beta Arrives
Major improvements to software management.

Canonical Announces Latest Ubuntu Core for IoT
Now offers 10 years of support.

GitHub Offers Free Private Repositories
Popular source code collaboration site makes a major change to feature set.

Linus Torvalds Welcomes 2019 with Linux 5.x
Better support for GPUs and CPUs.