Magic squares of doubly even order: Difference between revisions
Content deleted Content added
Adding a python version |
Added Julia language |
||
Line 973: | Line 973: | ||
24 122 123 21 20 126 127 17 16 130 131 13 |
24 122 123 21 20 126 127 17 16 130 131 13 |
||
133 11 10 136 137 7 6 140 141 3 2 144</pre> |
133 11 10 136 137 7 6 140 141 3 2 144</pre> |
||
=={{header|Julia}}== |
|||
<lang julia># v0.6 |
|||
function magicsquaredoubleeven(order::Int) |
|||
if order % 4 != 0; error("the order must be divisible by 4") end |
|||
sqr = Matrix{Int}(order, order) |
|||
mul = div(order, 4) |
|||
ext = vcat(1:mul, order-mul+1:order) |
|||
isext(i::Int, j::Int) = (i in ext) == (j in ext) |
|||
boolsqr = collect(isext(i, j) for i in 1:order, j in 1:order) |
|||
for i in linearindices(sqr) |
|||
if boolsqr[i]; sqr[i] = i end |
|||
if !boolsqr[end+1-i]; sqr[end+1-i] = i end |
|||
end |
|||
return sqr |
|||
end |
|||
for n in (4, 8, 12) |
|||
magicconst = div(n ^ 3 + n, 2) |
|||
sq = magicsquaredoubleeven(n) |
|||
println("Order: $n; magic constant: $magicconst.\nSquare:") |
|||
for r in 1:n, c in 1:n |
|||
@printf("%4i", sq[r, c]) |
|||
if c == n; println() end |
|||
end |
|||
println() |
|||
end</lang> |
|||
{{out}} |
|||
<pre>Order: 4; magic constant: 34. |
|||
Square: |
|||
1 12 8 13 |
|||
15 6 10 3 |
|||
14 7 11 2 |
|||
4 9 5 16 |
|||
Order: 8; magic constant: 260. |
|||
Square: |
|||
1 9 48 40 32 24 49 57 |
|||
2 10 47 39 31 23 50 58 |
|||
62 54 19 27 35 43 14 6 |
|||
61 53 20 28 36 44 13 5 |
|||
60 52 21 29 37 45 12 4 |
|||
59 51 22 30 38 46 11 3 |
|||
7 15 42 34 26 18 55 63 |
|||
8 16 41 33 25 17 56 64 |
|||
Order: 12; magic constant: 870. |
|||
Square: |
|||
1 13 25 108 96 84 72 60 48 109 121 133 |
|||
2 14 26 107 95 83 71 59 47 110 122 134 |
|||
3 15 27 106 94 82 70 58 46 111 123 135 |
|||
141 129 117 40 52 64 76 88 100 33 21 9 |
|||
140 128 116 41 53 65 77 89 101 32 20 8 |
|||
139 127 115 42 54 66 78 90 102 31 19 7 |
|||
138 126 114 43 55 67 79 91 103 30 18 6 |
|||
137 125 113 44 56 68 80 92 104 29 17 5 |
|||
136 124 112 45 57 69 81 93 105 28 16 4 |
|||
10 22 34 99 87 75 63 51 39 118 130 142 |
|||
11 23 35 98 86 74 62 50 38 119 131 143 |
|||
12 24 36 97 85 73 61 49 37 120 132 144</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |