import java.util.ArrayList; import java.util.Date; 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 printCSequence(long from) { // while (true) { // System.out.println(from + ", "); // from = nextCollatz(from); // if (from == 1) { // return; // } // } // } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long cura, curb, met; int a = scanner.nextInt(); ////TEST: // printCSequence(a); ////ENDTEST int b = scanner.nextInt(); Date startTime = new Date(); Date endTime = new Date(); while (!(a == 0 && b == 0)) { List alist = new ArrayList(); List blist = new ArrayList(); alist.add(new Long(a)); blist.add(new Long(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 long nextCollatz(long n) { if (n == 1) { return 1; } if (n % 2 == 0) { return n / 2; } else { return 3 * n + 1; } } }