Package 'magicrect'

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

Help Index


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×22 \times 2, and single row/column orders other than 1×11 \times 1, 1×21 \times 2 and 2×12 \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×qp \times q contains each integer 1,,pq1, \dots, pq exactly once, with every row sum equal to q(pq+1)/2q(pq+1)/2 and every column sum equal to p(pq+1)/2p(pq+1)/2. A nearly magic rectangle (defined for pp even, qq 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×qp \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×qp \times q. A magic rectangle exists iff pp and qq have the same parity, excluding 2×22 \times 2 and single row/column orders other than 1×11 \times 1. A nearly magic rectangle exists iff pp and qq have opposite parity and the odd dimension is at least 3 (or the order is 1×21 \times 2 / 2×12 \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,,pq1, \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 magic_rectangle.

Value

"magic", "nearly magic", or "neither".

Examples

verify_rectangle(magic_rectangle(5, 7))   # "magic"
verify_rectangle(matrix(1:6, 2, 3))       # "neither"