is.magic {magic}R Documentation

Various tests for the magicness of a square

Description

Returns TRUE if the square is magic, semimagic, panmagic, associative, normal. If argument give.answers is TRUE, also returns additional information about the sums.

Usage

is.magic(m,give.answers=FALSE)
is.panmagic(m,give.answers=FALSE)
is.semimagic(m,give.answers=FALSE)
is.associative(m)
is.normal(m)
is.mostperfect(m,give.answers=FALSE)
is.2x2.correct(m,give.answers=FALSE)
is.diagonally.correct(m,give.answers=FALSE)

Arguments

m The square to be tested
give.answers Boolean, with TRUE meaning return additional information about the sums (see details).

Details

A {em semimagic square} is one all of whose row sums equal all its columnwise sums (ie the magic constant).

A {em magic square} is a semimagic square with the sum of both unbroken diagonals equal to the magic constant.

A {em panmagic square} is a magic square all of whose broken diagonals sum to the magic constant. Ollerenshaw calls this a ``pandiagonal'' square.

A {em most perfect} square has all 2-by-2 arrays anywhere within the square summing to 2S where S=n^2+1; and all pairs of integers n/2 distant along the same major (NW-SE) diagonal sum to S (note that the S used here differs from Ollerenshaw's because her squares are numbered starting at zero). The first condition is tested by is.2x2.correct and the second by is.diagonally.correct.

All most perfect squares are panmagic.

A {em normal square} is one that contains n^2 consecutive integers (typically starting at 0 or 1).

Value

Returns TRUE if the square is semimagic, etc.
If give.answers is taken as an argument and is TRUE, return a list of at least five elements. The first element of the list is the answer: it is TRUE if the square is (semimagic, magic, panmagic). Elements 2-5 give the result of a call to allsums(), viz: rowwise and columwise sums; and broken major (ie NW-SE) and minor (ie NE-SW) diagonal sums.
Function is.diagonally.correct() also returns the sums of elements distant n/2 along a major diagonal (diag.sums); and function is.2x2.correct() returns the sum of each 2x2 submatrix (tbt.sums). Function is.mostperfect() returns both of these.

Author(s)

Robin K. S. Hankin

References

http://mathworld.wolfram.com/MagicSquare.html

See Also

minmax,is.perfect,is.semimagichypercube

Examples

is.magic(magic(4))
f <- function(n){is.magic(magic(n))}
all(sapply(3:50,f))

is.panmagic(panmagic.4())
is.panmagic(panmagic.8())

data(Ollerenshaw)
is.mostperfect(Ollerenshaw)

proper.magic <- function(m){is.magic(m) & minmax(c(1,diff(sort(m))))}
proper.magic(magic(20))

[Package Contents]