R/is_magic_square.R

Defines functions is_magic_square

Documented in is_magic_square

#' Verify magic square properties
#'
#' @param M Square matrix
#'
#' @return TRUE if M satisfies the magic square condition
#' @export
#'
#' @examples
#' M=YangConway(m = 2, d_type = "unit", template_set = 1)
#' is_magic_square(M)
is_magic_square <- function(M) {
  n <- nrow(M)
  target <- n * (n^2 + 1) / 2
  all(rowSums(M) == target) &&
    all(colSums(M) == target) &&
    sum(diag(M)) == target &&
    sum(diag(M[, n:1])) == target
}

Try the YangHuiMagic package in your browser

Any scripts or data that you put into this service are public.

YangHuiMagic documentation built on March 23, 2026, 5:07 p.m.