Magic squares of odd order: Difference between revisions

Add Draco
(Add Draco)
Line 1,341:
=={{header|Delphi}}==
See [https://www.rosettacode.org/wiki/Magic_squares_of_odd_order#Pascal Pascal].
 
=={{header|Draco}}==
<lang draco>proc inc(word n, order) word: if n=order-1 then 0 else n+1 fi corp
proc dec(word n, order) word: if n=0 then order-1 else n-1 fi corp
 
proc odd_magic_square([*,*]word square) void:
word order, x, nx, y, ny, i;
order := dim(square,1);
for x from 0 upto order-1 do
for y from 0 upto order-1 do
square[x,y] := 0
od
od;
x := order/2;
y := 0;
for i from 1 upto order*order do
square[x,y] := i;
nx := inc(x,order);
ny := dec(y,order);
if square[nx,ny] = 0 then
x := nx;
y := ny
else
y := inc(y,order)
fi
od
corp
 
proc digit_count(word n) word:
word count;
count := 0;
while n > 0 do
count := count + 1;
n := n / 10
od;
count
corp
 
proc print_magic_square([*,*]word square) void:
word order, max, col_size, magic, x, y;
order := dim(square,1);
max := order*order;
col_size := digit_count(max) + 1;
magic := 0;
for x from 0 upto order-1 do magic := magic + square[x,0] od;
writeln("Magic square of order ",order," with magic number ",magic,":");
for y from 0 upto order-1 do
for x from 0 upto order-1 do write(square[x,y]:col_size) od;
writeln()
od;
writeln()
corp
 
proc main() void:
[1,1]word sq1;
[3,3]word sq3;
[5,5]word sq5;
[7,7]word sq7;
odd_magic_square(sq1);
odd_magic_square(sq3);
odd_magic_square(sq5);
odd_magic_square(sq7);
print_magic_square(sq1);
print_magic_square(sq3);
print_magic_square(sq5);
print_magic_square(sq7)
corp</lang>
{{out}}
<pre>Magic square of order 1 with magic number 1:
1
 
Magic square of order 3 with magic number 15:
8 1 6
3 5 7
4 9 2
 
Magic square of order 5 with magic number 65:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
 
Magic square of order 7 with magic number 175:
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20</pre>
 
=={{header|EchoLisp}}==
2,114

edits