Learn R Programming

magic (version 1.3-20)

allsums: Row, column, and two diagonal sums of arrays

Description

Returns all rowsums, all columnsums, and all (broken) diagonal sums of a putative magic square.

Usage

allsums(m,FUN=sum)

Arguments

m
The square to be tested
FUN
Function, defaulting to sum, to be applied to the square rowwise, columnwise, and diagonalwise

Value

  • Returns a list of four elements. In the following, sums means the result of applying FUN().
  • rowsumsAll $n$ row sums
  • colsumsAll $n$ column sums
  • majorsAll $n$ broken major diagonals (northwest-southeast). First element is the long (unbroken) major diagonal, tested by is.magic()
  • minorsAll $n$ broken minor diagonals (northeast-southwest). First element is the long (unbroken) minor diagonal.

See Also

is.magic, is.semimagic, is.panmagic

Examples

Run this code
allsums(magic(7))
allsums(magic(7),FUN=max)
allsums(magic(7),FUN=function(x){x[1:2]})
  # shows how the minor diagonals are ordered: first [1,n] to [n,1] then
  # [1,n+1] to [n,2] etc.

allsums(magic.prime(7),sort)
  # beware! compare apply(magic(7),1,sort) with apply(magic(7),2,sort)

Run the code above in your browser using DataLab