• Welcome to Jose's Read Only Forum 2023.
 

Magic Squares

Started by Charles Pegge, December 30, 2022, 10:31:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

Trick 37 - Amazing 4 x 4 Magic Square

https://www.bing.com/videos/search?q=magic+square+24x24&docid=608051469956361536&mid=70039A3E6FDBEC42795370039A3E6FDBEC427953&view=detail&FORM=VIRE

youtube
https://www.youtube.com/watch?v=5STJvYz-gLI&t=720s
'
14   1    12   7
11   8    13   2
5    10   3    16
4    15   6    9



Magic Squares by Brian Hadley

https://www.youtube.com/watch?v=jWutSv3_nlE

Charles Pegge

#1
I thought it would be interesting to try encoding these techniques for generating magic squares.


This prog is for n={odd integer}


'04/01/2023
'magic squares of odd order (3,5,7,9,11 ...)
'
'INPUT
'=====
uses console
int m,n,v,w,x,y,px,py
print "Magic Square of odd order" cr cr
print "Enter odd integer (3..13) "
n=input
print cr
'
'CALULATE
=========
m=n*n
redim int a(n,n)
x=n*0.5+0.5
y=1
v=1
do
  a[y,x]=v
  py=y : px=x
  y-- : x++
  if y<1 then y=y+n
  if x>n then x=x-n
  if a[y,x]>0 then y=py+1 : x=px
  v++
  if v>m then exit do
loop
'
'DISPLAY
'=======
for y=1 to n
  v=0
  for x=1 to n
    v+=a[y,x]
    print a[y,x] tab
  next
  print v cr
next
print cr
for x=1 to n
  v=0
  for y=1 to n
    v+=a[y,x]
    if x=y then w+=a[y,x]
  next
    print v tab
next
print w cr
wait

Charles Pegge

Also known as doubly-even magic squares


'n divisible by 4

'INPUT
'=====
uses console
int m,n,v,w,x,y,px,py
print "Magic Square of doubly even order" cr cr
print "Enter integer divisible by 4 (4 8 12 16) "
n=input
print cr
'
'CALCULATE
'=========
int b[4,4]
b={ -1,0,0,-1, 0,-1,-1,0,  0,-1,-1,0, -1,0,0,-1 }
'
redim int a[n,n]
py=1
px=1
v=1
w=n*n
for y=1 to n
  for x=1 to n
    if b[py,px]<0
      a[y,x]=v
    else
      a[y,x]=w
    endif
    v++ : w--
    px++
    if px>4 then px=1
  next
  py++
  if py>4 then py=1
next
gosub displaySquare
wait
del a
del b
end

'DISPLAY
'=======
displaySquare: 'subroutine
for y=1 to n
  v=0 : w=0
  for x=1 to n
    v+=a[y,x]
    print a[y,x] tab
  next
  print v cr
next
print cr
for x=1 to n
  v=0
  for y=1 to n
    v+=a[y,x]
    if x=y then w+=a[y,x]
  next
  print v tab
next
print w cr
ret

Charles Pegge

#3
Magic squares of Singly even order, are divided into four squares of odd order. these are combined followed by various column swaps:


'05/01/2023
'magic squares (divisible by 2 but not 4)
uses console
int e,m,n,r,v,w,x,y,px,py

'INPUT
'=====
print "Magic Square of even order" cr cr
print "Enter even integer NOT divisible by 4 (6 10 14) "
r=input
print cr
'
'CALCULATE
'=========
n=r/2
m=n*n
redim int a(r,r)
x=n*0.5+0.5
y=1
v=1
do
  a[y,x]=v           'top left
  a[y+n,x+n]=v+m     'bottom right
  a[y  ,x+n]=v+m+m   'top right
  a[y+n,x  ]=v+m+m+m 'bottom left
  py=y : px=x
  y-- : x++
  if y<1 then y=y+n
  if x>n then x=x-n
  if a[y,x]>0 then y=py+1 : x=px
  v++
  if v>m then exit do
loop
'blocks swap
e=n\2
'left side
for x=1 to e
  for y=1 to n
    swap a[y,x],a[y+n,x]
  next
next
'right side
for x=r-e+2 to r
  for y=1 to n
    swap a[y,x],a[y+n,x]
  next
next
swap a[e+1,e],a[e+1+n,e]
swap a[e+1,e+1],a[e+1+n,e+1]
n=r
gosub displaySquare
wait
del a
end

'DISPLAY
'=======
displaySquare: 'subroutine
for y=1 to n
  v=0 : w=0
  for x=1 to n
    v+=a[y,x]
    print a[y,x] tab
  next
  print v cr
next
print cr
for x=1 to n
  v=0
  for y=1 to n
    v+=a[y,x]
    if x=y then w+=a[y,x]
  next
  print v tab
next
print w cr
ret

Charles Pegge

#4
The three algorithms brought together, to create magic squares with sides of 3 cells  or more.

Due to the typical console screen size, the practical limit is 14.


'07/01/2023
'magic squares of positive integer order (3..)
uses console
print "Magic Squares" cr

begin:
======
'
int e=0,m=0,n=0,r=0,v=0,w=0,x=0,y=0,px=0,py=0
'
'INPUT
'=====
print cr
print "Enter even positive integer (3..14) "
n=input
print cr
'
'SELECT
'======

select n
case <0 : end
case 0  : end
case 1  : 'allowed
end select
'
redim int a(n,n) clear

select n and 3

case 2: 'SINGLY EVEN
'
'CALCULATE
'=========
r=n
n=r/2
m=n*n
x=n*0.5+0.5
y=1
v=1
do
  a[y,x]=v           'top left
  a[y+n,x+n]=v+m     'bottom right
  a[y  ,x+n]=v+m+m   'top right
  a[y+n,x  ]=v+m+m+m 'bottom left
  py=y : px=x
  y-- : x++
  if y<1 then y=y+n
  if x>n then x=x-n
  if a[y,x]>0 then y=py+1 : x=px
  v++
  if v>m then exit do
loop
'blocks swap
e=n\2
'left side
for x=1 to e
  for y=1 to n
    swap a[y,x],a[y+n,x]
  next
next
'right side
for x=r-e+2 to r
  for y=1 to n
    swap a[y,x],a[y+n,x]
  next
next
swap a[e+1,e],a[e+1+n,e]
swap a[e+1,e+1],a[e+1+n,e+1]
n=r
gosub displaySquare
goto begin


case 0: 'DOUBLY EVEN
'
'n divisible by 4
'
'CALCULATE
'=========
int b[4,4]
b={ -1,0,0,-1, 0,-1,-1,0,  0,-1,-1,0, -1,0,0,-1 }
'
py=1
px=1
v=1
w=n*n
for y=1 to n
  for x=1 to n
    if b[py,px]<0
      a[y,x]=v
    else
      a[y,x]=w
    endif
    v++ : w--
    px++
    if px>4 then px=1
  next
  py++
  if py>4 then py=1
next
gosub displaySquare
goto begin


case 1,3: 'ODD
'
'04/01/2023
'magic squares of odd order (3,5,7,9,11 ...)
'
'CALULATE
=========
m=n*n
x=n*0.5+0.5
y=1
v=1
do
  a[y,x]=v
  py=y : px=x
  y-- : x++
  if y<1 then y=y+n
  if x>n then x=x-n
  if a[y,x]>0 then y=py+1 : x=px
  v++
  if v>m then exit do
loop
gosub displaySquare

end select
goto begin


'DISPLAY
'=======
displaySquare: 'subroutine
for y=1 to n
  v=0 : w=0
  for x=1 to n
    v+=a[y,x]
    print a[y,x] tab
  next
  print v cr 'row totals
next
print cr
for x=1 to n
  v=0
  for y=1 to n
    v+=a[y,x]
    if x=y then w+=a[y,x] 'diagonal from top left
  next
  print v tab 'column totals
next
print w cr 'diagonal total
ret


Roland Stowasser

It is interesting that magic squares have been known for 4000 years. I was only aware of 2 constructions of magic squares:

4*4 square:

  1   2   3  4
  5   6   7  8
  9 10 11 12
13 14 15 16  -- no magic square.

Reverse the order of the diagonals:

16   2   3 13
  5 11 10   8
  9   7   6 12
  4 14 15   1 -- This is already a magic square.

Swapping the middle columns:

16   3   2 13
  5 10 11   8
  9   6   7 12
  4 1514   1

This corresponds to the magic square of Dürer's engraving "Melencolia I" of the year 1514.
https://www.albrecht-duerer-apokalypse.de/sein-werk/die-drei-meisterstiche/melencolia-i/

At school we had to read Goethe's bestseller "Faust", where Mephisto cast his magic spell, the "Hexeneinmaleins". Some people interpreted this spell as the construction of this magic square:

3*3 square:

4 9 2
3 5 7
8 1 6

I wish I had had a better math teacher during that time (or Wikipedia and YouTube would have been available).