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

NixOS 21.11 Now Available for Download
NixOS “Porcupine” has been made available for installation and includes numerous improvements.

KDE Plasma Developers Introduce a GNOMELike Overview
A new overview effect makes it easier for users to interact with both Plasma Activities and Virtual Desktops.

Rocky Linux 8.5 Now Available with Secure Boot Support
The latest iteration of CentOS alternative, Rocky Linux has arrived and includes numerous updates as well as support for Secure Boot.

System76 Developing a New Desktop Environment
The makers of Pop!_OS are currently in the early stages of creating a brand new, nonGNOME, desktop environment.

Hetzner Opens New Location in the USA
The Hetzner costeffective Cloud solution has arrived in the United States to offer cloud servers that offer solid performance without a high price.

KDE Plasma 5.24 Introduces Fingerprint Reader Support
The next release of the KDE Plasma desktop environment will include support for fingerprint readers for session authentication and more.

Ubuntu 21.10 Released and Finally Includes Gnome 40
The most anticipated update to Ubuntu is finally here and the workflow couldn't be better.

Linux Can Now Run on Apple’s M1 Chipset
Linux users looking to take advantage of Apple Silicon are about to get their wish with the M1 chipset.

MX Linux 21 RC Candidate Available For Testing
The MX Linux development team has released the RC images for MX Linux 21 for testing purposes.

Fedora 35 is Shaping Up to Be an Impressive Release
Although the beta version of Fedora’s upcoming release is behind schedule, the Linux distribution is still slotted for October and will include numerous bug fixes and some exciting new features.