Given a number $n$, what is the minimal number of operations $+1, -1, /2, /3$ required to reach $0$?

139 Views Asked by At

Given a number $n > 0$, what is the minimal number of operations to reduce this number to $0$ ?

We can use these operations:

  • $+1$
  • $-1$
  • $/2$ (when number is divisible by $2$)
  • $/3$ (when number is divisible by $3$)

It would be awesome if you would also provide a proof.

2

There are 2 best solutions below

4
On

If the answer is $f(n)$, then $f(n)=1+\min(f(n-1),f(n+1),f(n/2),f(n/3))$.

Try using induction and see if you can find a pattern. For example the first few are $f(0)=0,f(1)=1,f(2)=2,f(3)=2$.

It might be hard to find a nice general solution though because I think this problem has connections to Catalan's conjecture and ABC conjecture.

6
On

This is not a solution but an extended comment.

Here is a quite tempting algorithm:

  • if $n$ is divisible by $3$, divide it by $3$. Otherwise :

    $\qquad\bullet$ if $n$ is divisible by $2$ then divide it by $2$. Otherwise :

    $\qquad \qquad \bullet$ $n$ is either $6k+1$ (in which case subtract $1$) or $6k-1$ (in which case add $1$).

Iterate.