Solving seemingly simple math problems using Perl
5x5 Test Run
The soughtafter groups in the math puzzle can now be put together very easily from this megasquare in a way that avoids overlap between participants on several consecutive days. Because these are only 5x5 squares, the method can only generate five groups each with four (n1) participants for five days: one teacher (of a total of five), one male student (also of five) and two female students (out of 10). Figure 7 shows the result, which is practically just a simple transformation of the entries from the megasquare in Figure 6.
The script in Listing 1 [5] moves from left to right and from top to bottom through the entries and translates them into people. The entry 1111
thus becomes i1 b1 g1 g6
, because it relates to the first teacher, the first boy, the first girl from the first group, and the first girl from the second group. The entry 3524
accordingly becomes i3 b5 g2 g9
, and so on.
Listing 1
combiprime
That's How to Do It
But, how does the script initially generate the four orthogonally arranged Latin squares from which the group arrangement can then be easily derived? The process is pretty simple, as it concerns 5x5 squares and five is a prime number.
Trust Is Good
The x and y values printed in black along the edges of the squares in Figure 5 run from 0 to 4 and are in mixedup order (0,2,3,4,1), because order does not matter to the algorithm. Each element of the matrix is computed by the simple formula (ay+x) mod 5+1, where a is set to 1 for the first square, to 2 for the second, and so on.
This process results in the following value for the element that is in the second column of the third row of the fourth square: The y value of the third row of the fourth square is 3, the x value of the second column is 2, and the value for a in the fourth square is 4, this returns 4x3+2=14. Reduced to modulo 5, 4 is left over, then 1 is added – because the sequence of values is from 1 to 5, not from 0 to 4 – giving a final result of 5.
Listing 1 implements this formula in the for
loops from line 14 and then starts outputting the combinations in the final part from line 37. The whole thing executes very quickly because the calculations are simple and the algorithm is linear, without any form of backtracking.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News

A New Linux Distribution has been Released
EmperorOS is a new Linux distribution focused on programming, developing, and data science.

Ubuntu Groovy Gorilla Beta Available
The beta version for Ubuntu’s latest release is now available for testing.

Microsoft is Finally Set to Release Edge Browser for Linux
Chromiumbased MS Edge browser will soon be available for testing on Linux.

Lenovo Now Offering Ubuntu 20.04 as an Option
Lenovo is making good on its promise to support Linux as a preinstall option.

Linux Deepin 20 Released
One of the most beautiful Linux distributions on the market has been refreshed.

A New Version of Zorin OS is Now Available
Fanfavorite, Zorin OS 15.3 is officially available.

Lenovo Now Offering Fedora Linux as an Option
Lenovo is making good on their Linux promise by adding Fedora into the mix.

System76 Launches New HighEnd Laptop
System76 has unleashed a beast of a mobile workstation.

Mozilla Lays Off Staff, Receives More Cash
In a restructuring move, Mozilla lays off 250 employees, but then inks large deal with Google.

LibreOffice 7 Now Available
The LibreOffice 7 office suite is now available with important compatibility improvements.