/* 1 6 2 11 7 3 11 24 7 20 3 16 12 8 4 4 12 25 8 16 21 17 13 9 5 --> 17 5 13 21 9 22 18 14 10 10 18 1 14 22 23 19 15 23 6 19 2 15 24 20 25 */ class MagicSquare { public static final int dim=3; /* Dimension of matrix */ public static double[][] a = new double[dim][dim]; public static int iter; public static void main (String args[]) { for (int ipr = 0; ipr < 5; ipr++) { long start = System.currentTimeMillis(); // iter = 100000; iter = 10000; test(); long end = System.currentTimeMillis(); System.out.println("Total time = "+ (end-start)*0.001); /* for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) System.out.print(a[i][j] + " " ); System.out.println(" "); }; */ System.out.println(" " ); } } public static void test() { double[][] magic = new double[dim][dim]; double[] rr = new double[dim]; double[] rr1 = new double[dim]; int x = (dim - 1)/2 + (dim + 1); int y = -(dim - 1)/2 + (dim - 1); double summa = dim * (dim*dim + 1) /2; int n = 1; for (int i = 0; i < dim; i++) { x = x - (dim + 1); y = y - (dim - 1); for (int j = 0; j < dim; j++) { if (x >= dim) magic[x-dim][y] = n/summa; else if (x < 0 ) magic[x+dim][y] = n/summa; else if (y >= dim) magic[x][y-dim] = n/summa; else if (y < 0 ) magic[x][y+dim] = n/summa; else magic[x][y] = n/summa; n++; x++; y++; } } for (int i = 0; i < dim; i++) { for (int k = 0; k < dim; k++) rr[k] = magic[i][k]; for (int iiii = 0; iiii < iter; iiii++) { for (int j = 0; j < dim; j++) { rr1[j] = 0.0; for (int k = 0; k < dim; k++) rr1[j] = rr1[j] + rr[k]*magic[k][j]; }; for (int k = 0; k < dim; k++) rr[k] = rr1[k]; } for (int k = 0; k < dim; k++) a[i][k] = rr[k]; }; } }