import java.math.BigInteger; import java.util.HashSet; import java.util.Set; /** * A321442. * @author Sean A. Irvine */ public class A321442 { // This has fairly close agreement with the table in the paper, but does not exactly // reproduce the table. It is unclear if I am missing some condition or if the // original table is wrong (it was computed by hand). The first disagreement is // for n0=11 which I get 21 and the table claims 28. private static final BigInteger TWO = BigInteger.valueOf(2); private static final BigInteger THREE = BigInteger.valueOf(3); private BigInteger mN = BigInteger.ONE; private boolean isEven(final BigInteger n) { return !n.testBit(0); } private boolean isOddPowerOf2(BigInteger n) { boolean oddPower = false; while (!BigInteger.ZERO.equals(n) && isEven(n)) { n = n.shiftRight(1); oddPower = !oddPower; } return BigInteger.ONE.equals(n) && oddPower; } public BigInteger next() { mN = mN.add(TWO); if (BigInteger.ONE.equals(mN)) { return BigInteger.ZERO; } long c = 0; boolean jacksTurn = true; Set live = new HashSet<>(); live.add(mN); while (true) { final Set next = new HashSet<>(); for (final BigInteger v : live) { if (jacksTurn && !BigInteger.ONE.equals(v) && ((!isEven(v) && v.compareTo(mN) < 0) || isOddPowerOf2(v))) { return BigInteger.valueOf(c / 2); } if (isEven(v)) { next.add(v.shiftRight(1)); } else if (!BigInteger.ONE.equals(v) && jacksTurn) { final BigInteger t = v.multiply(THREE); next.add(t.add(BigInteger.ONE)); next.add(t.subtract(BigInteger.ONE)); } } live = next; //System.out.println(live); jacksTurn = !jacksTurn; ++c; } } public static void main(final String[] args) { final A321442 seq = new A321442(); long n = 1; while(true) { n += 2; System.out.println(n + " " + seq.next()); } } }