| Type: | Package |
| Title: | Construct Magic Rectangles and Nearly Magic Rectangles |
| Version: | 1.0.0 |
| 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. |
| License: | MIT + file LICENSE |
| URL: | https://github.com/Arijitray2/magic-rectangles, https://arijitray2.github.io/magic-rectangles/ |
| BugReports: | https://github.com/Arijitray2/magic-rectangles/issues |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.2 |
| NeedsCompilation: | no |
| Packaged: | 2026-07-10 15:54:51 UTC; root |
| Author: | Arijit Ray [aut, cre] |
| Maintainer: | Arijit Ray <ray.asokekumar@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-07-19 13:10:13 UTC |
Construct a magic rectangle or nearly magic rectangle of order p x q
Description
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 (2 \times 2, and single row/column orders other than
1 \times 1, 1 \times 2 and 2 \times 1).
Usage
magic_rectangle(p, q)
Arguments
p |
Number of rows (positive integer). |
q |
Number of columns (positive integer). |
Details
A magic rectangle of order p \times q contains each integer
1, \dots, pq exactly once, with every row sum equal to
q(pq+1)/2 and every column sum equal to p(pq+1)/2. A nearly
magic rectangle (defined for p even, q 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.
Value
An object of class "magicrect": a list with components
matrix (the p \times q integer matrix), type
("magic" or "nearly magic"), p, q,
row_sums and col_sums.
References
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.
Examples
magic_rectangle(3, 5) # odd x odd magic rectangle
magic_rectangle(8, 10) # even x even magic rectangle
magic_rectangle(6, 7) # nearly magic rectangle
Classify which rectangle exists for a given order
Description
Determines whether a magic rectangle, a nearly magic rectangle, or neither
exists for order p \times q. A magic rectangle exists iff p and
q have the same parity, excluding 2 \times 2 and single
row/column orders other than 1 \times 1. A nearly magic rectangle
exists iff p and q have opposite parity and the odd dimension is
at least 3 (or the order is 1 \times 2 / 2 \times 1).
Usage
rectangle_type(p, q)
Arguments
p |
Number of rows (positive integer). |
q |
Number of columns (positive integer). |
Value
One of "magic", "nearly magic", "none".
Examples
rectangle_type(4, 6) # "magic"
rectangle_type(4, 7) # "nearly magic"
rectangle_type(2, 2) # "none"
Verify the defining properties of a (nearly) magic rectangle
Description
Checks from first principles that M contains each integer
1, \dots, pq 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).
Usage
verify_rectangle(M)
Arguments
M |
A matrix, or an object returned by |
Value
"magic", "nearly magic", or "neither".
Examples
verify_rectangle(magic_rectangle(5, 7)) # "magic"
verify_rectangle(matrix(1:6, 2, 3)) # "neither"