import java.util.ArrayList; import java.util.List; import java.util.Scanner; /* * 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); int cura, curb, met; int a = scanner.nextInt(); int b = scanner.nextInt(); while (!(a == 0 && b == 0)) { List alist = new ArrayList(); List blist = new ArrayList(); alist.add(a); blist.add(b); while (true) { cura = nextCollatz(alist.get(alist.size() - 1)); curb = nextCollatz(blist.get(blist.size() - 1)); alist.add(cura); blist.add(curb); if (alist.contains(curb)) { met = curb; break; } 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, alist.indexOf(met), b, blist.indexOf(met), met)); a = scanner.nextInt(); b = scanner.nextInt(); } } public static int nextCollatz(int n) { if (n == 1) { return 1; } if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } }