1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
if not modules then modules = {} end modules["t-squares"] = {
version = 20240223,
comment = "Magic and Latin squares",
author = "Jairo A. del Rio",
copyright = "Jairo A. del Rio",
license = "MIT License",
}
local interfaces = interfaces
local implement = interfaces.implement
local magic_reporter = logs.reporter("squares", "magic")
local latin_reporter = logs.reporter("squares", "latin")
local random = math.random
local context = context
-- https://www.iupindia.in/910/IJCM_Magic_Square_Construction_Algorithms34.pdf
-- https://arxiv.org/pdf/1402.3273.pdf
-- https://en.wikipedia.org/wiki/Conway%27s_LUX_method_for_magic_squares
-- Helpers
local function init(s)
local q = {}
for j = 1, s do
q[j] = {}
local c = q[j]
for i = 1, s do
c[i] = 0
end
end
return q
end
-- Odd numbers
local function magic_01(n)
local res = init(n)
local i, j = 1 + (n >> 1), 1
local k, l
res[i][j] = 1
for key = 2, n * n do
k = i % n + 1
l = 2 <= j and j - 1 or n
if res[k][l] > 0 then
j = j % n + 1
else
i, j = k, l
end
res[i][j] = key
end
return res
end
-- LUX method
local function magic_02(N)
local res = init(N)
local n = N // 2
local lux = init(n)
local L, U, X = 1, 2, 4
local x1, x2, x3, x4 = 0, -1, 0, -1
for i = 1, n do
for j = 1, n // 2 + 1 do
lux[i][j] = L
end
end
for i = 1, n do
lux[i][n // 2 + 2] = U
end
for j = n // 2 + 3, n do
for i = 1, n do
lux[i][j] = X
end
end
lux[n // 2 + 1][n // 2 + 1] = U
lux[n // 2 + 1][n // 2 + 2] = L
local i, j = 1 + (n >> 1), 1
local k, l
res[2 * i + x1][2 * j - 1] = 1
res[2 * i + x2][2 * j] = 2
res[2 * i + x3][2 * j] = 3
res[2 * i + x4][2 * j - 1] = 4
for key = 2, n * n do
k = i % n + 1
l = 2 <= j and j - 1 or n
if res[2 * k][2 * l] > 0 then
j = j % n + 1
else
i, j = k, l
end
if lux[i][j] == L then
x1, x2, x3, x4 = 0, -1, 0, -1
elseif lux[i][j] == U then
x1, x2, x3, x4 = -1, -1, 0, 0
else
x1, x2, x3, x4 = -1, 0, -1, 0
end
res[2 * i + x1][2 * j - 1] = 4 * (key - 1) + 1
res[2 * i + x2][2 * j] = 4 * (key - 1) + 2
res[2 * i + x3][2 * j] = 4 * (key - 1) + 3
res[2 * i + x4][2 * j - 1] = 4 * key
end
return res
end
local function magic_03(n)
local res = init(n)
for x = 1, n, 4 do
for y = 1, n, 4 do
local q = 0
for i = x, x + 3 do
q = q + 1
local q1 = 0
for j = y, y + 3 do
q1 = q1 + 1
if i == j or i + j == 5 or q + q1 == 5 or q == q1 then
res[i][j] = n * (i - 1) + j
else
res[i][j] = n * n - ((i - 1) * n + j) + 1
end
end
end
end
end
return res
end
local currentsquare = nil
local function magicsquare(n)
local r = n & 3
if r == 0 then
return magic_03(n)
elseif r == 2 then
return magic_02(n)
end
return magic_01(n)
end
local bTR, eTR = context.bTR, context.eTR
local bTD, eTD = context.bTD, context.eTD
implement({
name = "magicsquarecell",
arguments = { "integer", "integer" },
actions = function(x, y)
context(currentsquare and currentsquare[x][y] or "")
end,
})
implement({
name = "magicsquareinit",
arguments = { "integer" },
actions = function(n)
if n > 0 and n ~= 2 then
currentsquare = magicsquare(n)
else
magic_reporter(("Invalid number %d: nothing will be provided"):format(n))
end
end,
})
implement({
name = "magicsquarereset",
actions = function()
currentsquare = nil
end,
})
implement({
name = "magicsquare",
actions = function()
local r = currentsquare
if r then
local n = #r
for j = 1, n do
bTR()
for i = 1, n do
bTD()
context(r[i][j])
eTD()
end
eTR()
end
else
magic_reporter("Magic square not initialized...")
return
end
end,
})
local function rand2(a, b)
if random(2) == 1 then
return a, b
else
return b, a
end
end
local function latinsquare(n)
local xy = {}
local xz = {}
local yz = {}
for i = 1, n do
xy[i] = {}
xz[i] = {}
yz[i] = {}
for j = 1, n do
xy[i][j] = 0
xz[i][j] = 0
yz[i][j] = 0
end
end
for i = 1, n do
for j = 1, n do
local k = (i + j - 2) % n + 1
xy[i][j] = k
xz[i][k] = j
yz[j][k] = i
end
end
local mxy, mxz, myz = 0, 0, 0
local m = { 0, 0, 0 }
local proper = true
local minIter = n * n * n
local iter = 0
while iter < minIter or not proper do
local i, j, k, i2, j2, k2
local i2_, j2_, k2_
if proper then
i, j, k = random(n), random(n), random(n)
while xy[i][j] == k do
i, j, k = random(n), random(n), random(n)
end
i2 = yz[j][k]
j2 = xz[i][k]
k2 = xy[i][j]
i2_, j2_, k2_ = i, j, k
else
i, j, k = m[1], m[2], m[3]
i2, i2_ = rand2(yz[j][k], myz)
j2, j2_ = rand2(xz[i][k], mxz)
k2, k2_ = rand2(xy[i][j], mxy)
end
proper = xy[i2][j2] == k2
if not proper then
m = { i2, j2, k2 }
mxy = xy[i2][j2]
myz = yz[j2][k2]
mxz = xz[i2][k2]
end
xy[i][j] = k2_
xy[i][j2] = k2
xy[i2][j] = k2
xy[i2][j2] = k
yz[j][k] = i2_
yz[j][k2] = i2
yz[j2][k] = i2
yz[j2][k2] = i
xz[i][k] = j2_
xz[i][k2] = j2
xz[i2][k] = j2
xz[i2][k2] = j
iter = iter + 1
end
return xy
end
local currentlatin = nil
implement({
name = "latinsquarecell",
arguments = { "integer", "integer" },
actions = function(x, y)
context(currentlatin and currentlatin[x][y] or "")
end,
})
implement({
name = "latinsquareinit",
arguments = { "integer" },
actions = function(n)
if n > 0 then
currentlatin = latinsquare(n)
else
latin_reporter(("Invalid number %d: nothing will be provided"):format(n))
end
end,
})
implement({
name = "latinsquarereset",
actions = function()
currentlatin = nil
end,
})
implement({
name = "latinsquare",
actions = function()
local r = currentlatin
if r then
local n = #r
for j = 1, n do
bTR()
for i = 1, n do
bTD()
context(r[i][j])
eTD()
end
eTR()
end
else
latin_reporter("Latin square not initialized...")
return
end
end,
})
|