import java.util.Scanner; import java.io.File; import java.io.PrintStream; import java.io.FileNotFoundException; /** * Solution to 3n+1 Sequence problem. * @author Dave Reed */ public class Sequence { public static void main(String[] args) throws FileNotFoundException { Scanner infile = new Scanner(new File("input2.txt")); PrintStream outfile = new PrintStream("output2.txt"); while (infile.hasNextInt()) { int first = infile.nextInt(); int second = infile.nextInt(); outfile.println(first + " " + second + " " + Sequence.maxLength(Math.min(first, second), Math.max(first, second))); } } public static int maxLength(int low, int high) { int maxLen = 0; for (int i = low; i <= high; i++) { int nextLen = Sequence.seqLength(i); if (nextLen > maxLen) { maxLen = nextLen; } } return maxLen; } public static int seqLength(int start) { int count = 1; int num = start; while (num != 1) { if (num % 2 == 0) { num /= 2; } else { num = 3*num + 1; } count++; } return count; } }