Primitive Calculator - Dynamic Approach
<p>I'm having some trouble getting the correct solution for the following problem: </p>

<blockquote>
  <p>Your goal is given a positive integer n, find the minimum number of
  operations needed to obtain the number n starting from the number 1.</p>
</blockquote>

<p>More specifically the test case I have in the comments below.</p>

<pre><code> # Failed case #3/16: (Wrong answer)
    # got: 15 expected: 14
    # Input:
    # 96234
    #
    # Your output:
    # 15
    # 1 2 4 5 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234
    # Correct output:
    # 14
    # 1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234
    #  (Time used: 0.10/5.50, memory used: 8601600/134217728.)


    def optimal_sequence(n):
        sequence = []

        while n &gt;= 1:
            sequence.append(n)

            if n % 3 == 0:
                n = n // 3
                optimal_sequence(n)

            elif n % 2 == 0:
               n = n // 2
               optimal_sequence(n)

            else:
               n = n - 1
               optimal_sequence(n)

        return reversed(sequence)

    input = sys.stdin.read()
    n = int(input)
    sequence = list(optimal_sequence(n))
    print(len(sequence) - 1)
    for x in sequence:
        print(x, end=' ')
</code></pre>

<p>It looks like I should be outputting 9 where I'm outputting 4 &amp; 5 but I'm not sure why this isn't the case. What's the best way to troubleshoot this problem?</p>