// Computes a longest syracuse sequence whose values are all less than 10000. import element.ConsoleWindow; import java.util.Random; public class critical extends Thread { static Semaphore threadController; static Semaphore max; static Random gen; static ConsoleWindow c; long value; int depth; static int maxdepth; public critical() { this(1,1); } public critical(int depth, long value) { this.depth = depth; this.value = value; } public void run() { threadController.acquire(); /* ... if (...) new critical().start(); ... if (...) new critical().start(); ... */ max.acquire(); if (depth > maxdepth) { maxdepth = depth; System.out.println(value+": "+depth); } max.release(); if ((0 == ((value-1)%3)) && (1 == ((value-1)/3)%2) && (1 < ((value-1)/3))) { new critical(depth+1,(value-1)/3).start(); } if (2*value < 10000) { new critical(depth+1,value*2).start(); } threadController.release(); } public static void main(String args[]) { max = new Semaphore(); gen = new Random(); threadController = new Semaphore(10); new critical().start(); } } /* 1: 1 2: 2 4: 3 8: 4 16: 5 32: 6 64: 7 128: 8 256: 9 512: 10 160: 11 336: 12 682: 13 212: 14 424: 15 848: 16 282: 17 564: 18 1128: 19 2260: 20 4512: 21 240: 22 3013: 23 6026: 24 12052: 25 24096: 26 48192: 27 96384: 28 192768: 29 69036: 30 138072: 31 276144: 32 552288: 33 1104576: 34 2209152: 35 122731: 36 5922: 37 11844: 38 23688: 39 47376: 40 91392: 41 6006: 42 12012: 43 24024: 44 48048: 45 96096: 46 177067: 47 354134: 48 128379: 49 256758: 50 3075636: 51 944358: 52 1888716: 53 629571: 54 1259142: 55 2518284: 56 911297: 57 1822594: 58 607531: 59 1215062: 60 2430124: 61 810041: 62 1619442: 63 3238884: 64 1080054: 65 340313: 66 680626: 67 226875: 68 453750: 69 907500: 70 1624178: 71 3248356: 72 1082785: 73 2165570: 74 852602: 75 1705204: 76 */