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
combi-verify
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.
Nine-Element 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 B-male students and the C-, D-, and E-female students. He then formed the nine-element 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 sub-classes 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
combi-design
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.linux-magazine.com/pub/listings/magazine/179
- Thirty-six officers problem: http://en.wikipedia.org/wiki/Thirty-six_officers_problem
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.