View source: R/YangRamanujan.R
| YangRamanujan | R Documentation |
Construct doubly even order magic square (Yang-Hui · Dürer · Ramanujan inspired)
YangRamanujan(n, params = "natural", L1 = NULL, L2 = NULL)
n |
Positive integer, order = 4n |
params |
Parameter setting. Can be a list containing a11, d1, d2, d12, d21 (d22 is optional, automatically set to d12+d21). Predefined strings: - "natural": natural square parameters (a11=1, d1=1, d2=4n, d12=2n, d21=2n*4n) - "type1" : second row of Table 3 (a11=1, d1=1, d2=2n, d12=(2n)^2, d21=2*(2n)^2) - "type2" : third row of Table 3 (a11=1, d1=4n, d2=1, d12=?, d21=?, d22=?) [values incomplete, for illustration only] |
L1 |
Sets of rows/columns to flip (1‑based), default automatically generated symmetric sets |
L2 |
Sets of rows/columns to flip (1‑based), default automatically generated symmetric sets |
4n x 4n magic square matrix
# Example 1: Ramanujan's 8th order magic square (matrix 4 in the paper)
# Using natural square parameters, L1 = L2 = {2,3,6,7}
cat("\n===== Ramanujan's 8th order magic square =====\n")
ramanujan8 <- YangRamanujan(n = 2,params = "natural",L1 = c(2, 3, 6, 7),L2 = c(2, 3, 6, 7))
print(ramanujan8)
cat("Is it a magic square: ", is_magic_square(ramanujan8), "\n")
# Example 2: One of Yang-Hui's 4th order magic squares (matrix 2 in the paper)
# For n=1, use natural square, L1 = L2 = {2,3} (rows/columns 1..4 for order 4)
cat("\n===== Yang-Hui 4th order magic square (matrix 2) =====\n")
yanghui4 <- YangRamanujan(n = 1, params = "natural",L1 = c(2, 3),L2 = c(2, 3))
print(yanghui4)
cat("Is it a magic square: ", is_magic_square(yanghui4), "\n")
# Example 3: Dürer's 4th order magic square (matrix 3 in the paper)
# According to Table 1, use specific parameters (n=1) and L1 = L2 = {2,3}
# Parameters from row (22) in Table 1: a11=1, d1=-1, d2=-8, d12=-2, d21=-4, d22=-6
cat("\n===== Dürer's 4th order magic square (matrix 3) =====\n")
duerer_params <- list(a11 = 1, d1 = 1, d2 = 8, d12 = 2, d21 = 4)
duerer4 <- YangRamanujan(n = 1,params = duerer_params,L1 = c(2, 3),L2 = c(2, 3))
print(duerer4)
cat("Is it a magic square: ", is_magic_square(duerer4), "\n")
# Example 4: Custom 12th order magic square (n=3), using natural square, default symmetric L
cat("\n===== Custom 12th order magic square (natural square, default L) =====\n")
magic12 <- YangRamanujan(n = 3, params = "natural")
# Print only first 6 rows to save space
print(magic12)
cat("Is it a magic square: ", is_magic_square(magic12), "\n")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.