Suppose ms a two-dimensional square indexed from O to n — 1 in both dimensions. For an odd number n, a Simple algorithm for constructing magic squares exists: the square is constructed by filling in the numbers me To n2 in a run, starting at; = (n— 1)/2 and = n—1 and incrementing and by one in each step, modulo n. Every n-the step j is decremented by one and we left unchanged. In the square to the right, we start at, = 1 And j = 2 and put 1 in that Iodation incrementing ¡ makes ¡ 2 and incrementing j makes j = 3, where we Need to put2 (grey above), but modulo 3 makes j = 0 we find the location of 3 similarly but then need to Move one down and continue there with 4, etc., went a function magic square (n) that returns a square as A list of lists (but the function itself does not print it) and a function print square (s) that takes a list of lists And prints the square such that the numbers are aligned and there are two spaces between the numbers.
Get Free Quote!
338 Experts Online