Skip to contents

Creates normal magic squares of any order \(>2\). Uses the appropriate method depending on n modulo 4.

Usage

magic(n)

Arguments

n

Order of magic square. If a vector, return a list whose \(i\)-th element is a magic square of order n[i]

Details

Calls either magic.2np1(), magic.4n(), or magic.4np2() depending on the value of n. Returns a magic square in standard format (compare the magic.2np1() et seq, which return the square as generated by the direct algorithm).

References

William H. Benson and Oswald Jacoby. New recreations with magic squares. Dover 1976.

Author

Robin K. S. Hankin

Examples

magic(6)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    7    6   35   34   15   14
#> [2,]    8    5   33   36   16   13
#> [3,]   27   26   19   18   11   10
#> [4,]   25   28   20   17    9   12
#> [5,]   23   22    3    2   31   30
#> [6,]   21   24    1    4   29   32
all(is.magic(magic(3:10)))
#> [1] TRUE

## The first eigenvalue of a magic square is equal to the magic constant:
eigen(magic(10),FALSE,TRUE)$values[1] - magic.constant(10)
#> [1] -4.547474e-13

## The sum of the eigenvalues of a magic square after the first is zero:
sum(eigen(magic(10),FALSE,TRUE)$values[2:10])
#> [1] 3.026023e-13