magic-square

NumPy equivalent of Matlab's magic()

扶醉桌前 提交于 2020-06-11 20:04:18
问题 In Ocatave / Matlab, I can use magic() to get a magic square, e.g., magic(4) 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 Definition: A magic square is an N×N grid of numbers in which the entries in each row, column and main diagonal sum to the same number (equal to N(N^2+1)/2 ). How can I generate the same using NumPy? 回答1: This implementation follows Matlab's and should give exactly the same results with the following exception: it throws an error if n < 3 rather than return a non-magic square [

How do I make my code run in a loop and ask the user “Try again Yes or no?”

橙三吉。 提交于 2019-12-20 07:56:16
问题 import java.io.*; public class Magic{ static final int maxsize = 50; public static void main (String [] args) throws IOException{ int i, j, k, l, n, key; boolean n_ok; String line; int [] [] square = new int [maxsize] [maxsize]; BufferedReader KeybIn = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.print("Size of square? "); line = KeybIn.readLine(); n = Integer.parseInt(line); n_ok = (1<=n) & (n<=maxsize+1) & (n%2==1); if ( n_ok ){ for (i=0;i<n;i++) for (j=0;j<n;j++)

Magic Square Program (C++)

让人想犯罪 __ 提交于 2019-12-19 03:07:10
问题 For those unfamiliar with the classic magic square algorithm: A magic square is a two dimensional array (n x n) which contains a numerical value between the values 1 and n^2 in each location. Each value may appear only once. Furthermore, the sum of each row, column and diagonal must be the same. The input should be odd as I am writing an odd magic square solution. I have completed the problem but as of now it has an unknown bug (logic? output?) that has been vexing me for the past hour. The

prolog I have to make a program that calculates the magic matrix permutate

旧城冷巷雨未停 提交于 2019-12-13 16:19:18
问题 I have to make a program that calculates the magic matrix, I have made ​​my code and it works but my permute is very slow. I need one that is faster someone can help me Please This is the code: diabolico([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]) :- permutar([1,14,3,16,5,12,13,15,9,10,11,6,7,2,8,4],[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]), A+B+C+D=:=34, E+F+G+H=:=34, I+J+K+L=:=34, M+N+O+P=:=34, A+E+I+M=:=34, B+F+J+N=:=34, C+G+K+O=:=34, D+H+L+P=:=34, M+B+G+L=:=34, I+N+C+H=:=34, E+J+O+D=:=34, A+F+K+P=:=34, P

Magic Square program help (Java) [closed]

时光毁灭记忆、已成空白 提交于 2019-12-13 05:25:46
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . This is homework for my AP Computer Science class dealing with 2D Arrays. Basically what the program does is input values into a Magic Square (all rows, columns, and diagonals add up to be the same number) and

Is it possible to condense my code into less than 3 lines?

℡╲_俬逩灬. 提交于 2019-12-12 04:12:46
问题 public class MagicSquare { public static int[][] grid = new int[3][3]; public static int i = 0; public static int j = 0; public static void main(String[] args) { int x = 1; int y = 2; int z = 0; while(z < 9) { int holdx = x; int holdy = y; z++; x++; y--; if(x == 3) { x = 0; } if(y == -1) { y = 2; } if(y == 3) { y = 0; } if(grid[x][y] == 0) { grid[x][y] = z; } else { holdy++; if(holdy == 3) { holdy = 0; } grid[holdx][holdy] = z; x = holdx; y = holdy; } } for(int i = 0; i < 3; i++) { System.out

Stack overflow error bruteforcing a magic square. Any possible solution?

99封情书 提交于 2019-12-12 00:28:13
问题 Here my problem, for an exercise I need to generate a magic square by bruteforcing it in backtracking. I thought it could be useful to allocate the matrix as a vector and a function that changes the coordinates. As you can imagine even with a 3x3 magic square it gave me a stack overflow problem. Debugging it i discovered it happen, more or less, at half of the generating, more precisely where the function chk_magic(int *m, int n) call change_coord(i, j, m, n); . Here it is the entire code,

C - 2D Array - Magic Square order 4

这一生的挚爱 提交于 2019-12-11 02:56:33
问题 114 void fillDoubly(int square[20][20], int n){ 115 116 int i, j, k=0, l=0, counter=0, test[400]={0}, diff=n/4-1; 117 118 for(i=0;i<n;i++) //first nested for loops for part 1) 119 for(j=0;j<n;j++){ 120 counter++; 121 if( i=j || j=(n-1-i) ){ 122 { 123 square[i][j] = counter; 124 test[counter-1] = 1; 125 } 126 } 127 } 128 129 for(i=n-1;i>=0;i--) // for part 2) 130 for(j=n-1;j>=0;j--){ 131 if(square[i][j]==0){ 132 while(test[k]!=0){ 133 k++; 134 } 135 test[k]=1; 136 square[i][j]=k+1; 137 } 138 }