Magic squares of doubly even order: Difference between revisions
Content deleted Content added
Added C implementation. |
|||
Line 655: | Line 655: | ||
Magic constant: 260</pre> |
Magic constant: 260</pre> |
||
=={{header|Elena}}== |
|||
{{trans|C#}} |
|||
ELENA 3.3 : |
|||
<lang elena>import system'routines. |
|||
import extensions. |
|||
import extensions'routines. |
|||
MagicSquareDoublyEven = (:n)<int> |
|||
[ |
|||
if((n < 4)||(n mod(4) != 0)) |
|||
[ InvalidArgumentException new:"base must be a positive multiple of 4" ]. |
|||
int bits := 09669h. |
|||
int size := n * n. |
|||
int mult := n / 4. |
|||
var result := IntMatrix new(n,n). |
|||
int r := 0. |
|||
int i := 0. |
|||
while (r < n) |
|||
[ |
|||
int c := 0. |
|||
while(c < n) |
|||
[ |
|||
int bitPos := c / mult + (r / mult) * 4. |
|||
result[r][c] := (bits && (1 << bitPos) != 0)iif(i+1,size - i). |
|||
i += 1. |
|||
c += 1. |
|||
]. |
|||
r += 1. |
|||
]. |
|||
^ result |
|||
]. |
|||
program = |
|||
[ |
|||
int n := 8. |
|||
MagicSquareDoublyEven(n) forEach(printingLn). |
|||
console printLine; printLine("Magic constant: ",(n * n + 1) * n / 2). |
|||
].</lang> |
|||
{{out}} |
|||
<pre> |
|||
1,2,62,61,60,59,7,8 |
|||
9,10,54,53,52,51,15,16 |
|||
48,47,19,20,21,22,42,41 |
|||
40,39,27,28,29,30,34,33 |
|||
32,31,35,36,37,38,26,25 |
|||
24,23,43,44,45,46,18,17 |
|||
49,50,14,13,12,11,55,56 |
|||
57,58,6,5,4,3,63,64 |
|||
Magic constant: 260 |
|||
</pre> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |