public static PythagoreanTriple getInstance(long idx) {
        if (idx < 0) {
            throw new IllegalArgumentException(
                "idx must be zero or positive number");
        }

        if (0 == idx) {
            return START;
        }

        Stack<Integer> stack = new Stack<Integer>();

        while (idx > 0) {
            stack.push((int) ((idx - 1) % 3));
            idx = (idx - 1) / 3;
        }

        long m = 2;
        long n = 1;

        while (!stack.isEmpty()) {
            long t = m;

            switch (stack.pop()) {
            case 0: // U
                m = (2 * m) - n;
                n = t;

                break;

            case 1: // A
                m = (2 * m) + n;
                n = t;

                break;

            case 2: // D
                m += (2 * n);

                break;
            }

            idx = (idx - 1) / 3;
        }

        return new PythagoreanTriple(m, n);
    }