Construct Magic Rectangles and Nearly Magic Rectangles

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, ). 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.


magicrect

Construct magic rectangles and nearly magic rectangles of any order p × q for which one exists.

🔗 Live calculator: https://arijitray2.github.io/magic-rectangles/ — the same algorithms, running in your browser.

A magic rectangle of order p × q arranges the integers 1, …, pq so that every row sums to q(pq+1)/2 and every column sums to p(pq+1)/2. It exists precisely when p and q have the same parity — excluding the impossible 2 × 2 and single-row/column orders (Hagedorn 1999).

When p and q have opposite parity the magic constants are not integers, so a magic rectangle cannot exist; a nearly magic rectangle (Chai, Singh & Stufken 2019) exists instead: it uses 1, …, pq once each, has constant sums along one direction, and sums along the other direction differing by at most 1.

To our knowledge this is the first R package that constructs magic rectangles and nearly magic rectangles (the CRAN package magic covers magic squares and hypercubes only).

Installation

# install.packages("remotes")
remotes::install_github("Arijitray2/magic-rectangles")

Usage

library(magicrect)

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 (6 even, 7 odd)
magic_rectangle(7, 11)    # reproduces Chai-Das-Midha (2013), Section 3

rectangle_type(4, 7)      # "nearly magic"
rectangle_type(2, 2)      # "none" - the classical exception

r <- magic_rectangle(13, 19)
r$matrix                  # the 13 x 19 integer matrix
r$row_sums                # all equal q(pq+1)/2
verify_rectangle(r)       # "magic" - checked from first principles

Example output:

> magic_rectangle(3, 5)
3 x 5 magic rectangle

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    9    7   10   13
[2,]    8   11   14    2    5
[3,]   15    4    3   12    6

Row sums:     40 40 40
Column sums:  24 24 24 24 24

What is implemented

Order p × q Object Construction
both even magic rectangle De Los Reyes, Das, Midha & Vellaisamy (2009)
both odd magic rectangle Chai, Das & Midha (2013): pattern matrix G_p, within-column interchanges, and the Theorem 2.1 composition for q ≡ 0 (mod 3)
even × odd nearly magic rectangle Chai, Singh & Stufken (2019): Theorems 2.1, 2.3, 2.4
2 × 2 — provably impossible
1 × n, n > 2 — provably impossible

Correctness

tests/exhaustive.R (run automatically by R CMD check) constructs every order 1 ≤ p, q ≤ 40 and verifies each result from first principles: the entries are exactly {1, …, pq}, and the row/column sums satisfy the relevant definition. The construction also reproduces the worked 7 × 11 and 13 × 19 examples printed in Chai, Das & Midha (2013) digit for digit. The JavaScript port used by the website is additionally cross-checked to be byte-identical to the R output on all 843 constructible orders with p, q ≤ 30, and self-verified up to 100 × 100.

References

  1. T. R. Hagedorn (1999). Magic rectangles revisited. Discrete Mathematics 207, 65–72.
  2. J. P. De Los Reyes, A. Das, C. K. Midha, P. Vellaisamy (2009). On a method to construct magic rectangles of even order. Utilitas Mathematica 80, 277–284.
  3. F.-S. Chai, A. Das, C. Midha (2013). Construction of magic rectangles of odd order. Australasian Journal of Combinatorics 55(1), 131–144.
  4. F.-S. Chai, R. Singh, J. Stufken (2019). Nearly magic rectangles. Journal of Combinatorial Designs 27(6), 368–376.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("magicrect")

1.0.0 by Arijit Ray, 3 months ago


https://github.com/Arijitray2/magic-rectangles, https://arijitray2.github.io/magic-rectangles/


Report a bug at https://github.com/Arijitray2/magic-rectangles/issues


Browse source code at https://github.com/cran/magicrect


Authors: Arijit Ray [aut, cre]


Documentation:   PDF Manual  


MIT + file LICENSE license



See at CRAN