Magic squares of doubly even order: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) Rename Perl 6 -> Raku, alphabetize, minor clean-up |
|||
Line 551: | Line 551: | ||
133 134 135 9 8 7 6 5 4 142 143 144 |
133 134 135 9 8 7 6 5 4 142 143 144 |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
{{trans|Java}} |
|||
<lang csharp>using System; |
|||
namespace MagicSquareDoublyEven |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
int n = 8; |
|||
var result = MagicSquareDoublyEven(n); |
|||
for (int i = 0; i < result.GetLength(0); i++) |
|||
{ |
|||
for (int j = 0; j < result.GetLength(1); j++) |
|||
Console.Write("{0,2} ", result[i, j]); |
|||
Console.WriteLine(); |
|||
} |
|||
Console.WriteLine("\nMagic constant: {0} ", (n * n + 1) * n / 2); |
|||
Console.ReadLine(); |
|||
} |
|||
private static int[,] MagicSquareDoublyEven(int n) |
|||
{ |
|||
if (n < 4 || n % 4 != 0) |
|||
throw new ArgumentException("base must be a positive " |
|||
+ "multiple of 4"); |
|||
// pattern of count-up vs count-down zones |
|||
int bits = 0b1001_0110_0110_1001; |
|||
int size = n * n; |
|||
int mult = n / 4; // how many multiples of 4 |
|||
int[,] result = new int[n, n]; |
|||
for (int r = 0, i = 0; r < n; r++) |
|||
{ |
|||
for (int c = 0; c < n; c++, i++) |
|||
{ |
|||
int bitPos = c / mult + (r / mult) * 4; |
|||
result[r, c] = (bits & (1 << bitPos)) != 0 ? i + 1 : size - i; |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
}</lang> |
|||
<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|C++}}== |
=={{header|C++}}== |
||
Line 620: | Line 678: | ||
57 7 6 60 61 3 2 64 |
57 7 6 60 61 3 2 64 |
||
</pre> |
</pre> |
||
=={{header|C#}}== |
|||
{{trans|Java}} |
|||
<lang csharp>using System; |
|||
namespace MagicSquareDoublyEven |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
int n = 8; |
|||
var result = MagicSquareDoublyEven(n); |
|||
for (int i = 0; i < result.GetLength(0); i++) |
|||
{ |
|||
for (int j = 0; j < result.GetLength(1); j++) |
|||
Console.Write("{0,2} ", result[i, j]); |
|||
Console.WriteLine(); |
|||
} |
|||
Console.WriteLine("\nMagic constant: {0} ", (n * n + 1) * n / 2); |
|||
Console.ReadLine(); |
|||
} |
|||
private static int[,] MagicSquareDoublyEven(int n) |
|||
{ |
|||
if (n < 4 || n % 4 != 0) |
|||
throw new ArgumentException("base must be a positive " |
|||
+ "multiple of 4"); |
|||
// pattern of count-up vs count-down zones |
|||
int bits = 0b1001_0110_0110_1001; |
|||
int size = n * n; |
|||
int mult = n / 4; // how many multiples of 4 |
|||
int[,] result = new int[n, n]; |
|||
for (int r = 0, i = 0; r < n; r++) |
|||
{ |
|||
for (int c = 0; c < n; c++, i++) |
|||
{ |
|||
int bitPos = c / mult + (r / mult) * 4; |
|||
result[r, c] = (bits & (1 << bitPos)) != 0 ? i + 1 : size - i; |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
}</lang> |
|||
<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|D}}== |
=={{header|D}}== |
||
Line 729: | Line 729: | ||
Magic constant: 260</pre> |
Magic constant: 260</pre> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
Line 1,272: | Line 1,273: | ||
Magic constant: 260</pre> |
Magic constant: 260</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 1,644: | Line 1,644: | ||
See [[Magic_squares/Perl|Magic squares/Perl]] for a general magic square generator. |
See [[Magic_squares/Perl|Magic squares/Perl]] for a general magic square generator. |
||
<lang perl></lang> |
<lang perl></lang> |
||
=={{header|Perl 6}}== |
|||
See [[Magic_squares/Perl_6|Magic squares/Perl 6]] for a general magic square generator. |
|||
{{out}} |
|||
With a parameter of 8: |
|||
<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 |
|||
The magic number is 260</pre> |
|||
With a parameter of 12: |
|||
<pre> 1 2 3 141 140 139 138 137 136 10 11 12 |
|||
13 14 15 129 128 127 126 125 124 22 23 24 |
|||
25 26 27 117 116 115 114 113 112 34 35 36 |
|||
108 107 106 40 41 42 43 44 45 99 98 97 |
|||
96 95 94 52 53 54 55 56 57 87 86 85 |
|||
84 83 82 64 65 66 67 68 69 75 74 73 |
|||
72 71 70 76 77 78 79 80 81 63 62 61 |
|||
60 59 58 88 89 90 91 92 93 51 50 49 |
|||
48 47 46 100 101 102 103 104 105 39 38 37 |
|||
109 110 111 33 32 31 30 29 28 118 119 120 |
|||
121 122 123 21 20 19 18 17 16 130 131 132 |
|||
133 134 135 9 8 7 6 5 4 142 143 144 |
|||
The magic number is 870</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,152: | Line 2,122: | ||
[7,] 49 15 14 52 53 11 10 56 |
[7,] 49 15 14 52 53 11 10 56 |
||
[8,] 8 58 59 5 4 62 63 1</pre> |
[8,] 8 58 59 5 4 62 63 1</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
See [[Magic_squares/Perl_6|Magic squares/Perl 6]] for a general magic square generator. |
|||
{{out}} |
|||
With a parameter of 8: |
|||
<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 |
|||
The magic number is 260</pre> |
|||
With a parameter of 12: |
|||
<pre> 1 2 3 141 140 139 138 137 136 10 11 12 |
|||
13 14 15 129 128 127 126 125 124 22 23 24 |
|||
25 26 27 117 116 115 114 113 112 34 35 36 |
|||
108 107 106 40 41 42 43 44 45 99 98 97 |
|||
96 95 94 52 53 54 55 56 57 87 86 85 |
|||
84 83 82 64 65 66 67 68 69 75 74 73 |
|||
72 71 70 76 77 78 79 80 81 63 62 61 |
|||
60 59 58 88 89 90 91 92 93 51 50 49 |
|||
48 47 46 100 101 102 103 104 105 39 38 37 |
|||
109 110 111 33 32 31 30 29 28 118 119 120 |
|||
121 122 123 21 20 19 18 17 16 130 131 132 |
|||
133 134 135 9 8 7 6 5 4 142 143 144 |
|||
The magic number is 870</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |