Create an N by N magic square.
- Input:
- N -- an integer in some form, may be float or quotted.
- Output:
- an 'int32' N by N array -- the same magic square as in
Matlab and Octave magic(N) commands. In particular, the
Siamese method is used for odd N (but with a different
implementation.)
- Examples:
>>> from magic_square import *
>>> magic(4)
array([[16, 2, 3, 13],
[ 5, 11, 10, 8],
[ 9, 7, 6, 12],
[ 4, 14, 15, 1]])
>>> magic_constant()
34
>>> magic(5.0)
array([[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[ 4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])
>>> print magic('6')
[[35 1 6 26 19 24]
[ 3 32 7 21 23 25]
[31 9 2 22 27 20]
[ 8 28 33 17 10 15]
[30 5 34 12 14 16]
[ 4 36 29 13 18 11]]
>>> magic(2)
Traceback (most recent call last):
TypeError: No such magic squares exist.
>>> magic(0)
array([], shape=(0, 0), dtype=int32)
>>> magic_constant()
0
- Notes:
- The calculations for n close to 1000 are about 100--200
times faster than in Octave.
-
|