From: Peter Hickman Date: 2006-07-28T22:46:10+09:00 Subject: Re: For performance, write it in C - Part 2, comparing C, Ruby and Java pat eyler wrote: > Was the code for the Java and Ruby versions posted somewhere in > the other thread? (I must admit, I tuned a lot of that thread out > once it > became a shouting match about the relative speeds of C and Java.) > The Ruby version was posted in the previous thread by Simon. I didn't post the Java version because, code wise, it is pretty much a line for line translation of the C version. But so you can see what the code was like here is the 3 x 3 version. The 5 x 5 version is just too damn big for a post, being as it is 5449 lines long! public class Latin { private static int WidthOfBoard = 3; private static int NumberOfPermutations = 6; private static String[] OutputStrings = { "321", "231", "213", "312", "132", "123" }; private static boolean[][] Compared = new boolean[6][6]; private static int work[] = { 0, 0, 0 }; private static void addARow(int row) { if (row == WidthOfBoard) { for (int x = 0; x < WidthOfBoard; x++) { if (x == 0) { System.out.print(OutputStrings[work[x]]); } else { System.out.print(":" + OutputStrings[work[x]]); } } System.out.println(); } else { for (int x = 0; x < NumberOfPermutations; x++) { work[row] = x; boolean is_ok = true; if (row != 0) { for (int y = 0; y < row; y++) { if (Compared[work[row]][work[y]] != true) { is_ok = false; break; } } } if (is_ok == true) { addARow(row + 1); } } } } public static void main(String[] args) { // This nonsense is to get around the fact that Java will not allow // me to initialise an array in the declaration. Compared[0][2] = true; Compared[0][4] = true; Compared[1][3] = true; Compared[1][5] = true; Compared[2][0] = true; Compared[2][4] = true; Compared[3][1] = true; Compared[3][5] = true; Compared[4][0] = true; Compared[4][2] = true; Compared[5][1] = true; Compared[5][3] = true; addARow(0); } }