Magic cubes of order 2n+1
magiccube.2np1.Rd
Creates odd-order magic cubes
Examples
#try with m=3, n=2*3+1=7:
m <-7
n <- 2*m+1
apply(magiccube.2np1(m),c(1,2),sum)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [1,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [2,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [3,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [4,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [5,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [6,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [7,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [8,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [9,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [10,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [11,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [12,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [13,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [14,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [15,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [,13] [,14] [,15]
#> [1,] 25320 25320 25320
#> [2,] 25320 25320 25320
#> [3,] 25320 25320 25320
#> [4,] 25320 25320 25320
#> [5,] 25320 25320 25320
#> [6,] 25320 25320 25320
#> [7,] 25320 25320 25320
#> [8,] 25320 25320 25320
#> [9,] 25320 25320 25320
#> [10,] 25320 25320 25320
#> [11,] 25320 25320 25320
#> [12,] 25320 25320 25320
#> [13,] 25320 25320 25320
#> [14,] 25320 25320 25320
#> [15,] 25320 25320 25320
apply(magiccube.2np1(m),c(1,3),sum)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [1,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [2,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [3,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [4,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [5,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [6,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [7,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [8,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [9,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [10,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [11,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [12,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [13,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [14,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [15,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [,13] [,14] [,15]
#> [1,] 25320 25320 25320
#> [2,] 25320 25320 25320
#> [3,] 25320 25320 25320
#> [4,] 25320 25320 25320
#> [5,] 25320 25320 25320
#> [6,] 25320 25320 25320
#> [7,] 25320 25320 25320
#> [8,] 25320 25320 25320
#> [9,] 25320 25320 25320
#> [10,] 25320 25320 25320
#> [11,] 25320 25320 25320
#> [12,] 25320 25320 25320
#> [13,] 25320 25320 25320
#> [14,] 25320 25320 25320
#> [15,] 25320 25320 25320
apply(magiccube.2np1(m),c(2,3),sum)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [1,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [2,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [3,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [4,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [5,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [6,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [7,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [8,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [9,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [10,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [11,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [12,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [13,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [14,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [15,] 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320 25320
#> [,13] [,14] [,15]
#> [1,] 25320 25320 25320
#> [2,] 25320 25320 25320
#> [3,] 25320 25320 25320
#> [4,] 25320 25320 25320
#> [5,] 25320 25320 25320
#> [6,] 25320 25320 25320
#> [7,] 25320 25320 25320
#> [8,] 25320 25320 25320
#> [9,] 25320 25320 25320
#> [10,] 25320 25320 25320
#> [11,] 25320 25320 25320
#> [12,] 25320 25320 25320
#> [13,] 25320 25320 25320
#> [14,] 25320 25320 25320
#> [15,] 25320 25320 25320
#major diagonal checks out:
sum(magiccube.2np1(m)[matrix(1:n,n,3)])
#> [1] 25320
#now other diagonals:
b <- c(-1,1)
f <- function(dir,v){if(dir>0){return(v)}else{return(rev(v))}}
g <- function(jj){sum(magiccube.2np1(m)[sapply(jj,f,v=1:n)])}
apply(expand.grid(b,b,b),1,g) #each diagonal twice, once per direction.
#> [1] 25320 25320 25320 25320 25320 25320 25320 25320