Magic squares of doubly even order: Difference between revisions

Scala contribution added.
(Corrected my existing code)
(Scala contribution added.)
Line 1,703:
</pre>
 
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/bdTcGF3/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/gLhkwHHlRO6rPXg9U7MDzg Scastie (remote JVM)].
<lang Scala>object MagicSquareDoublyEven extends App {
private val n = 8
 
private def magicSquareDoublyEven(n: Int): Array[Array[Int]] = {
require(n >= 4 || n % 4 == 0, "Base must be a positive multiple of 4.")
 
// pattern of count-up vs count-down zones
val (bits, mult, result, size) = (38505, n / 4, Array.ofDim[Int](n, n), n * n)
var i = 0
 
for (r <- result.indices; c <- result(0).indices) {
def bitPos = c / mult + (r / mult) * 4
 
result(r)(c) = if ((bits & (1 << bitPos)) != 0) i + 1 else size - i
i += 1
}
result
}
 
magicSquareDoublyEven(n).foreach(row => println(row.map(x => f"$x%2s ").mkString))
println(f"---%nMagic constant: ${(n * n + 1) * n / 2}%d")
 
}</lang>
=={{header|VBScript}}==
{{trans|Java}}
Line 1,724 ⟶ 1,749:
wscript.echo "Magic constant=" & (n*n+1)*n/2</lang>
{{out}}
<pre>Magic square : 8 x 8
<pre>
Magic square : 8 x 8
1 2 62 61 60 59 7 8
9 10 54 53 52 51 15 16
Line 1,734 ⟶ 1,758:
49 50 14 13 12 11 55 56
57 58 6 5 4 3 63 64
Magic constant=260</pre>
</pre>
 
=={{header|zkl}}==
Anonymous user