General algorithm for a magic square

Click For Summary

Discussion Overview

The discussion revolves around algorithms for constructing magic squares of various sizes and with different properties, including the possibility of using arbitrary numbers instead of the standard sequence from 1 to n². Participants explore different methods for odd, double-even, and simple-even magic squares.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant inquires about the existence of a universal algorithm for creating magic squares of any size with a specified magic sum, and whether arbitrary numbers can be used.
  • Several participants reference a Wikipedia page but note its lack of comprehensive methods for constructing magic squares with arbitrary numbers.
  • One participant asserts that there are at least three distinct algorithms based on the parity of n: one for odd n, one for double-even n, and one for simple-even n.
  • Algorithms for generating odd, double-even, and simple-even magic squares are provided, with specific examples and code snippets in ARIBAS.
  • There is a discussion about what constitutes "general methods" for constructing magic squares, with differing interpretations among participants.

Areas of Agreement / Disagreement

Participants generally agree that multiple algorithms exist for different types of magic squares, but there is no consensus on a single universal method applicable to all cases. The discussion remains unresolved regarding the use of arbitrary numbers and the definition of "general methods."

Contextual Notes

Some algorithms provided depend on specific conditions, such as the parity of n or the requirement that n/2 must be odd for simple-even squares. There are also unresolved aspects regarding the flexibility of using arbitrary numbers in the construction of magic squares.

Vineeth T
Messages
31
Reaction score
0
Is there any algorithm to form a magic square of any size with a desired magic sum ?
Also can we make a magic square not only with the numbers from 1 to n2
but using any random numbers ?
 
Physics news on Phys.org
RamaWolf said:
For a quick overview look at: http://en.wikipedia.org/wiki/Magic_square

But in this webpage,no general methods of construction of a magic square of any numbers are given.
Can you give me a better one ?
 
There is not one algorithm for all n (a natural number); I think, there are at least 3:
for odd numbers, for double even ( ie for numbers with 4 as factor) and for even numbers.

Below you find my little algorithm (written in ARIBAS) to generate an odd magic square;
example for n = 11; for simplicity of the algorithm, a 'vector' is used to store the 'square' .


==> MagicSquareOdd(11).
-: (68, 81, 94, 107, 120, 1, 14, 27, 40, 53, 66, 80, 93, 106, 119, 11, 13, 26,
39, 52, 65, 67, 92, 105, 118, 10, 12, 25, 38, 51, 64, 77, 79, 104, 117, 9, 22,
24, 37, 50, 63, 76, 78, 91, 116, 8, 21, 23, 36, 49, 62, 75, 88, 90, 103, 7, 20,
33, 35, 48, 61, 74, 87, 89, 102, 115, 19, 32, 34, 47, 60, 73, 86, 99, 101, 114,
6, 31, 44, 46, 59, 72, 85, 98, 100, 113, 5, 18, 43, 45, 58, 71, 84, 97, 110,
112, 4, 17, 30, 55, 57, 70, 83, 96, 109, 111, 3, 16, 29, 42, 56, 69, 82, 95,
108, 121, 2, 15, 28, 41, 54)

Code:
   n1:=n - 1; n2:=n*n; n3:=n1 + n1 + 1; 
   realloc(a, n2);
   i:=1;
   j:= n1 div 2;
   a[j]:=i;
   while i < n2 do
      i:=i + 1;
      if j mod n = n1 then
         k:=j - n3;   
      else
         k:=j - n1;
      end;
      if k < 0 then
         k:=k + n2;
      end;
      if a[k] = 0 then
         j:=k;
      else
         j:=j + n;       
      end;
      a[j]:=i;
   end;
 
Vineeth T said:
But in this webpage,no general methods of construction of a magic square of any numbers are given.
Can you give me a better one ?
What do you mean by "general methods"? I consider "A method of constructing majic squares of even number of rows" and "Method for constructing a majic square of odd order" to be "general methods".
 
Below you find my little algorithm (written in ARIBAS) to generate an double-even magic square of side length n (ie 4 divides n);

example for n = 4; for simplicity of the algorithm, a 'vector' is used to store the 'square'


MagicSquareDoubleEven(4).
-: (16, 2, 3, 13, 5, 11, 10, 8, 9, 7, 6, 12, 4, 14, 15, 1)

Code:
   d:= n div 4; d2:=d + d; n2:=n * n;
   realloc(a,n2);
   k:=1 + n2; k2:=0;
   for i:=1 to d do
      for j:=1 to d do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k;
      end;
      for j:=1 to d2 do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k2;
      end;
      for j:=1 to d do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k;
      end;      
   end;
   for i:=1 to d2 do
      for j:=1 to d do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k2;
      end;
      for j:=1 to d2 do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k;
      end;
      for j:=1 to d do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k2;
      end;      
   end;  
   for i:=1 to d do
      for j:=1 to d do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k;
      end;
      for j:=1 to d2 do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k2;
      end;
      for j:=1 to d do
         k:=k - 1; k2:=k2 + 1; a[k2 - 1]:=k;
      end;      
   end;
 
Below you find my little algorithm (written in ARIBAS) to generate an simple-even magic square of side length n (ie n div 2 must be odd);

example for n = 6; the algorithm uses my 'MagicSquareOdd' (see above)
to form a matrix a and an auxiliary matrix 'aux' for the needed inter change of
some elements of matix a

MagicSquareSimpleEven(6)

((35, 1, 6, 26, 19, 24),
(3, 32, 7, 21, 23, 25),
(31, 9, 2, 22, 27, 20),
(8, 28, 33, 17, 10, 15),
(30, 5, 34, 12, 14, 16),
(4, 36, 29, 13, 18, 11))

A good companion for this algorithm was: http://www.hp-gramatke.de/magic_sq/index.htm

Code:
   k:= n div 2; k2:= k * k; d:= (k - 1) div 2;
   a:=alloc(array,n,alloc(array,n));
   aux:=alloc(array,k,alloc(array,n));
   for i:=0 to d - 1 do
      for j:=0 to d - 1 do
         aux[i][j]:=1;
      end;
   end;
   for j:=0 to d - 1 do
      aux[d][1+j]:=2;
   end;
   for i:=d + 1 to k - 1 do
      for j:=0 to d - 1 do
         aux[i][j]:=3;      
      end;
   end;
   for i:=0 to k - 1 do
      for j:=0 to d - 2 do
         aux[i][n-j-1]:=4;
      end;
   end;
   realloc(b, k2);
   b:=MagicSquareOdd(k);
   for i:=0 to k - 1 do
      for j:=0 to k - 1 do   
         a[i][j]:=b[i*k+j]; a[i][j+k]:=2*k2+b[i*k+j];
         a[i+k][j]:=3*k2+b[i*k+j]; a[i+k][j+k]:=k2+b[i*k+j];
      end;
   end;
   for i:=0 to k - 1 do
      for j:=0 to n - 1 do
         if aux[i][j] > 0 then
            d:=a[i][j]; a[i][j]:=a[i+k][j]; a[i+k][j]:=d;
         end;
      end;
   end;
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 68 ·
3
Replies
68
Views
12K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K