Magic squares of odd order: Difference between revisions

Add Cowgol
(Add BCPL)
(Add Cowgol)
Line 1,282:
11 18 25 2 9</pre>
 
=={{header|Cowgol}}==
{{trans|C}}
<lang cowgol>include "cowgol.coh";
 
sub magic(n: uint16) is
sub f(x: uint16, y: uint16): (r: uint16) is
r := (x + y*2 + 1) % n;
end sub;
sub cell(x: uint16, y: uint16): (c: uint16) is
c := f(n-x-1, y)*n + f(x, y) + 1;
end sub;
var y: uint16 := 0;
while y < n loop
var x: uint16 := 0;
loop
print_i16(cell(x, y));
x := x + 1;
if x == n then
print_nl();
break;
else
print_char('\t');
end if;
end loop;
y := y + 1;
end loop;
print_nl();
end sub;
 
var n: uint16 := 1;
while n <= 7 loop
print("Magic square of order ");
print_i16(n);
print(" with constant ");
print_i16((n*n+1)/2*n);
print(":\n");
magic(n);
n := n + 2;
end loop;</lang>
{{out}}
<pre>Magic square of order 1 with constant 1:
1
 
Magic square of order 3 with constant 15:
2 9 4
7 5 3
6 1 8
 
Magic square of order 5 with constant 65:
2 23 19 15 6
14 10 1 22 18
21 17 13 9 5
8 4 25 16 12
20 11 7 3 24
 
Magic square of order 7 with constant 175:
2 45 39 33 27 21 8
18 12 6 49 36 30 24
34 28 15 9 3 46 40
43 37 31 25 19 13 7
10 4 47 41 35 22 16
26 20 14 1 44 38 32
42 29 23 17 11 5 48</pre>
=={{header|D}}==
{{trans|Python}}
2,115

edits