/* Interpreter of the Turing Machine. An initial CURRENT_STATE of the Turing Machine is coded by 1, final CURRENT_STATE - by 99. The shift of the head to the right is designated through 1, to the left - through -1 . */ class tmmul2 { public static final int ltape=4400; /* Length of a tape */ public static final int nunits=2000; /* Quantity of units, not of zero */ public static int head; public static int state; /* Indexes in array of the instructions, */ /* an example - at the end of the program */ public static final int CURRENT_STATE = 0; public static final int CURRENT_SYMBOL = 1; public static final int NEXT_SYMBOL = 2; public static final int NEXT_STATE = 3; public static final int MOVE = 4; public static int[] tape = new int [ltape]; public static void main (String args[]) { head = ltape/2; /* the head in the middle of a tape */ for (int i = 0; i < ltape; i++) /* filling of zero */ tape[i] = 0; for (int i = head; i < head+nunits; i++) tape[i] = 1; long start = System.currentTimeMillis(); test(); long end = System.currentTimeMillis(); System.out.println("Total time = "+ (end-start)*0.001); System.out.println("Instructions : " ); System.out.println(" " ); System.out.println("Final Tape : " ); for (int i = 0; i < ltape; i++) System.out.print(tape[i]) ; } public static void test() { final int[][] instruction = { { 1 , 0 , 0 , 99 , 1 }, { 1 , 2 , 2 , 1 , 1 }, { 1 , 1 , 2 , 2 , -1 }, { 2 , 2 , 2 , 2 , -1 }, { 2 , 0 , 2 , 1 , 1 } }; final int ninstr = instruction.length; state = 1; while (state != 99) { int tapehead = tape[head]; for (int iinstr = 0; iinstr < ninstr; iinstr++) { int[] instr = instruction[iinstr]; if (state == instr[CURRENT_STATE] ) { if (tapehead == instr[CURRENT_SYMBOL]) { state=instr[NEXT_STATE]; tape[head]=instr[NEXT_SYMBOL]; head+=instr[MOVE]; }; }; }; } } }