Magic squares of odd order: Difference between revisions

Updated to work with Nim 1.4. Removed the '^' function.
(Updated to work with Nim 1.4. Removed the '^' function.)
Line 2,856:
{{trans|Python}}
<lang nim>import strutils
 
proc `^`*magic(base: int, expn: int): int =
varlet (base, exp)length = len(base,$(n exp* n))
result = 1
 
while exp != 0:
if (exp and 1) != 0:
result *= base
exp = exp shr 1
base *= base
 
proc magic(n) =
for row in 1 .. n:
for col in 1 .. n:
let cell = (n * ((row + col - 1 + n div 2) mod n) +
((row + 2 * col - 2) mod n) + 1)
stdout.write align($cell, len).align($(n^2))length)," "' '
echo ""
echo "\nAll sum to magic number ", ((n * n + 1) * n div 2)
for n in [53, 35, 7]:
echo "\nOrder ", n, "\n======="
proc magic(n) =</lang>
 
for n in [5, 3, 7]:
echo "\nOrder ",n,"\n======="
magic(n)</lang>
{{out}}
<pre>Order 53
=======
8 1 6
3 5 7
4 9 2
 
All sum to magic number 15
 
Order 35
=======
17 24 1 8 15
Line 2,889:
 
All sum to magic number 65
 
Order 3
=======
8 1 6
3 5 7
4 9 2
 
All sum to magic number 15
 
Order 7
Anonymous user