Magic squares of doubly even order: Difference between revisions
Content deleted Content added
m simplified t/mod |
|||
Line 1,858: | Line 1,858: | ||
57 58 6 5 4 3 63 64 |
57 58 6 5 4 3 63 64 |
||
Magic constant=260</pre> |
Magic constant=260</pre> |
||
=={{header|Visual Basic .NET}}== |
|||
{{trans|C#}} |
|||
<lang vbnet>Module MagicSquares |
|||
Function MagicSquareDoublyEven(n As Integer) As Integer(,) |
|||
If n < 4 OrElse n Mod 4 <> 0 Then |
|||
Throw New ArgumentException("base must be a positive multiple of 4") |
|||
End If |
|||
'pattern of count-up vs count-down zones |
|||
Dim bits = Convert.ToInt32("1001011001101001", 2) |
|||
Dim size = n * n |
|||
Dim mult As Integer = n / 4 ' how many multiples of 4 |
|||
Dim result(n - 1, n - 1) As Integer |
|||
Dim i = 0 |
|||
For r = 0 To n - 1 |
|||
For c = 0 To n - 1 |
|||
Dim bitPos As Integer = Math.Floor(c / mult) + Math.Floor(r / mult) * 4 |
|||
Console.Write("{0} ", bitPos) |
|||
Dim test = (bits And (1 << bitPos)) <> 0 |
|||
If test Then |
|||
result(r, c) = i + 1 |
|||
Else |
|||
result(r, c) = size - i |
|||
End If |
|||
i = i + 1 |
|||
Next |
|||
Console.WriteLine() |
|||
Next |
|||
Return result |
|||
End Function |
|||
Sub Main() |
|||
Dim n = 8 |
|||
Dim result = MagicSquareDoublyEven(n) |
|||
For i = 0 To result.GetLength(0) - 1 |
|||
For j = 0 To result.GetLength(1) - 1 |
|||
Console.Write("{0,2} ", result(i, j)) |
|||
Next |
|||
Console.WriteLine() |
|||
Next |
|||
Console.WriteLine() |
|||
Console.WriteLine("Magic constant: {0} ", (n * n + 1) * n / 2) |
|||
Console.ReadLine() |
|||
End Sub |
|||
End Module</lang> |
|||
{{out}} |
|||
<pre>0 0 1 1 2 2 3 3 |
|||
0 0 1 1 2 2 3 3 |
|||
4 4 5 5 6 6 7 7 |
|||
4 4 5 5 6 6 7 7 |
|||
8 8 9 9 10 10 11 11 |
|||
8 8 9 9 10 10 11 11 |
|||
12 12 13 13 14 14 15 15 |
|||
12 12 13 13 14 14 15 15 |
|||
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|zkl}}== |
=={{header|zkl}}== |