private static List<Integer> GetChain(
            int number,
            int root,
            HashSet<Integer> previousTerms,
            HashMap<Integer, Integer> nextNumberMemo) {
        if (_maxTerm < number) {
            return null;
        }

        boolean isAlreadyThere = previousTerms.contains(number);
        previousTerms.add(number);

        if (isAlreadyThere || number <= 1) {
            if (number == root) {
                ArrayList<Integer> result = new ArrayList<Integer>();
                result.add(number);
                return result;
            } else {
                return null;
            }
        }


        Integer nextNumber = nextNumberMemo.get(number);
        if (nextNumber == null && !nextNumberMemo.containsKey(number)) {
            nextNumber = GetNextNumber(number);
            nextNumberMemo.put(number, nextNumber);
        }

        if (nextNumber == null || _maxTerm <= nextNumber) {
            return null;
        }

        List<Integer> chain = GetChain(nextNumber, root, previousTerms, nextNumberMemo);

        if (chain == null) {
            return null;
        } else {
            chain.add(number);
            return chain;
        }
    }