# recursion and command line-arguments:in python magic squares

This should be written in Python. The program will determine all of the magic squares when given an n, display permutations that match the magic squares to the screen AND write it to a file.

It will only test when n == 3!!! But code it for n of ANY number!! It must come up with every possible arrangement (permutation) of numbers within the square. These are known as the permutations of that list of numbers. Each permutation needs to be checked to see if it has formed a magic square.

The program must:
Use command line arguments. At the command line the user must enter (in this order):
1. The name of the executable file,
2. n (which will ALWAYS be 3, but again could be run with other numbers)
and the name of the output file in which to write the squares.

Use a recursive permute to give all of the permutations of the square.

In other words, your function called "permute", must be a recursive function. So for each permutation, you should check to see if it is a magic square, display it and write it to the file. Write only the unique magic squares to the SCREEN and FILE. No duplicates allowed. Close any files that you have opened as soon as you have finished using them. Time how long your program takes to find the magic square and print that time at the end of the SAME file.

SAMPLE OUTPUT:

2 7 6
9 5 1
4 3 8

2 9 4
7 5 3
6 1 8

4 3 8
9 5 1
2 7 6

4 9 2
3 5 7
8 1 6

6 1 8
7 5 3
2 9 4

6 7 2
1 5 9
8 3 4

8 1 6
3 5 7
4 9 2

8 3 4
1 5 9
6 7 2

Total running time: 4.76 seconds.
this what i have so far and i dont know haow to use command lines and also problem with file reading
[code]import operator, random, sys, time, math
02
from math import*
03

04
square = []
05

06
def userinput(): #demands input from the user
07
return input('enter a value for n: ')
08

09
def isvalid(n): #checks whether n is valid
10
if n == 0 :
11
print 'invalid arguments for magic square'
12
return 0
13
elif n==2:
14
print 'invalid arguments for magic square'
15
return 0
16
elif n>3:
17
print 'warning: n is greater than 3 so it will take a long time to compute'
18
return 1
19
else:
20
return 1
21

22
def thesquare(n): #creates a NxN matrix
23
for i in range(n):
24
square.append([])
25
for j in range(n):
26
square[i].append(0)
27
return square
28

29
def thesum(n): #computes the required sum
30
s = (n*(n*n + 1))/2
31
return s
32

33
def allpossiblevals(n): #lists all possible values
34
vals = range(1, (n*n)+1, 1)
35
return vals
36

37
def allpermutations(vals): #lists all possible arrangements of all possible values
38
length = len(vals)
39
if length <= 1:
40
yield vals
41
else:
42
for permut in allpermutations(vals[1:]):
43
for i in range(len(permut)+1):
44
yield permut[:i]+vals[0:1]+permut[i:]
45

46
def perm(): #evaluates which permutations are valid magic squares
47
n = userinput()
48
v = isvalid(n)
49
if v == 1:
50
squares = thesquare(n)
51
a = allpossiblevals(n)
52
b = allpermutations(a)
53
c = list(B)
54
psize = len(c)
55
total = thesum(n)
56
for i in range(psize):
57
count = 0
58
for j in range(n):
59
for k in range(n):
60
squares[j][k] = c[i].pop()
61
for l in range(n):
62
if sum(square[l]) == total:
63
count = count + 1
64
transpose = zip(*square)
65
for m in range(n):
66
if sum((transpose)[m]) == total:
67
count = count + 1
68
if count == n+n:
69
print square

i dont know how to read and write to a file[/code]