import java.util.HashSet; import java.util.Scanner; import java.util.Set; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author cteam063 */ public class collatz { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long cura = 0, curb = 0; Item met = null; long a = scanner.nextLong(); long b = scanner.nextLong(); while (!(a == 0 && b == 0)) { int i = 1; Set aSet = new HashSet(); Set bSet = new HashSet(); Item lastA = new Item(a, 0); Item lastB = new Item(b, 0); aSet.add(lastA); bSet.add(lastB); int aSteps = 0; int bSteps = 0; while (true) { lastA = new Item(nextCollatz(lastA.getNumber()), i); lastB = new Item(nextCollatz(lastB.getNumber()), i); aSet.add(lastA); bSet.add(lastB); if (bSet.contains(lastA)) { met = lastA; aSteps = i; for (Item item : bSet) { if (item.equals(lastA)) { bSteps = item.position; } } break; } if (aSet.contains(lastB)) { met = lastB; bSteps = i; for (Item item : aSet) { if (item.equals(lastB)) { aSteps = item.position; } } break; } i++; } // List alist = new ArrayList(); // List blist = new ArrayList(); // alist.add(a); // blist.add(b); // while (true) { // long lastA = alist.get(alist.size() - 1); // long lastB = blist.get(blist.size() - 1); // if (lastA < lastB) { // if ((lastA % 2) == 1) { // cura = nextCollatz(lastA); // alist.add(cura); // if (blist.contains(cura)) { // met = cura; // break; // } // } else { // curb = nextCollatz(lastB); // blist.add(curb); // if (alist.contains(curb)) { // met = curb; // break; // } // } // } else { // if ((lastB % 2) == 1) { // curb = nextCollatz(lastB); // blist.add(curb); // if (alist.contains(curb)) { // met = curb; // break; // } // } else { // cura = nextCollatz(lastA); // alist.add(cura); // if (blist.contains(cura)) { // met = cura; // break; // } // } // } // } System.out.println(String.format("%d needs %d steps, %d needs %d steps, they meet at %d", a, aSteps, b, bSteps, met.getNumber())); a = scanner.nextLong(); b = scanner.nextLong(); } } public static long nextCollatz(long n) { if (n == 1) { return 1; } if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } public static class Item { public long number; public int position; public Item(long number, int position) { this.number = number; this.position = position; } public long getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Item other = (Item) obj; if (this.number != other.number) { return false; } return true; } @Override public int hashCode() { int hash = 3; hash = 13 * hash + (int) (this.number ^ (this.number >>> 32)); return hash; } }; }