The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

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.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.