| Title: | Construct Magic Rectangles and Nearly Magic Rectangles |
|---|---|
| Description: | Constructs a magic rectangle or a nearly magic rectangle of order p x q for every order for which one exists, together with existence classification and verification utilities. A magic rectangle arranges the integers 1 to p*q so that all row sums are equal and all column sums are equal; it exists exactly when p and q have the same parity, excluding 2 x 2 and degenerate single-row/column cases (Hagedorn, 1999, <doi:10.1016/S0012-365X(99)00041-2>). When p and q have opposite parity a nearly magic rectangle exists instead, with constant sums along one direction and sums differing by at most one along the other (Chai, Singh and Stufken, 2019, Journal of Combinatorial Designs 27(6), 368-376). Implements the constructions of De Los Reyes, Das, Midha and Vellaisamy (2009) for even by even orders, Chai, Das and Midha (2013) for odd by odd orders, and Chai, Singh and Stufken (2019) for the nearly magic (even by odd) case. |
| Authors: | Arijit Ray [aut, cre] |
| Maintainer: | Arijit Ray <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-07-19 15:44:23 UTC |
| Source: | https://github.com/cran/magicrect |
Returns a magic rectangle when one exists for the given order (p and
q of the same parity), and otherwise a nearly magic rectangle
(p, q of opposite parity). Throws an informative error for the
impossible orders (, and single row/column orders other than
, and ).
magic_rectangle(p, q)magic_rectangle(p, q)
p |
Number of rows (positive integer). |
q |
Number of columns (positive integer). |
A magic rectangle of order contains each integer
exactly once, with every row sum equal to
and every column sum equal to . A nearly
magic rectangle (defined for even, odd, or the transpose)
has constant column sums and row sums differing pairwise by at most 1.
The implementation follows De Los Reyes, Das, Midha and Vellaisamy (2009) for even by even orders, Chai, Das and Midha (2013) for odd by odd orders, and Chai, Singh and Stufken (2019) for nearly magic rectangles.
An object of class "magicrect": a list with components
matrix (the integer matrix), type
("magic" or "nearly magic"), p, q,
row_sums and col_sums.
Chai, F.-S., Das, A. and Midha, C. (2013). Construction of magic rectangles of odd order. Australasian Journal of Combinatorics, 55(1), 131–144.
Chai, F.-S., Singh, R. and Stufken, J. (2019). Nearly magic rectangles. Journal of Combinatorial Designs, 27(6), 368–376.
De Los Reyes, J. P., Das, A., Midha, C. K. and Vellaisamy, P. (2009). On a method to construct magic rectangles of even order. Utilitas Mathematica, 80, 277–284.
Hagedorn, T. R. (1999). Magic rectangles revisited. Discrete Mathematics, 207, 65–72.
magic_rectangle(3, 5) # odd x odd magic rectangle magic_rectangle(8, 10) # even x even magic rectangle magic_rectangle(6, 7) # nearly magic rectanglemagic_rectangle(3, 5) # odd x odd magic rectangle magic_rectangle(8, 10) # even x even magic rectangle magic_rectangle(6, 7) # nearly magic rectangle
Determines whether a magic rectangle, a nearly magic rectangle, or neither
exists for order . A magic rectangle exists iff and
have the same parity, excluding and single
row/column orders other than . A nearly magic rectangle
exists iff and have opposite parity and the odd dimension is
at least 3 (or the order is / ).
rectangle_type(p, q)rectangle_type(p, q)
p |
Number of rows (positive integer). |
q |
Number of columns (positive integer). |
One of "magic", "nearly magic", "none".
rectangle_type(4, 6) # "magic" rectangle_type(4, 7) # "nearly magic" rectangle_type(2, 2) # "none"rectangle_type(4, 6) # "magic" rectangle_type(4, 7) # "nearly magic" rectangle_type(2, 2) # "none"
Checks from first principles that M contains each integer
exactly once and that its row and column sums satisfy the
definition of a magic rectangle (all equal) or a nearly magic rectangle
(constant in one direction, differing by at most 1 in the other).
verify_rectangle(M)verify_rectangle(M)
M |
A matrix, or an object returned by |
"magic", "nearly magic", or "neither".
verify_rectangle(magic_rectangle(5, 7)) # "magic" verify_rectangle(matrix(1:6, 2, 3)) # "neither"verify_rectangle(magic_rectangle(5, 7)) # "magic" verify_rectangle(matrix(1:6, 2, 3)) # "neither"