subtract a number from its digits until it reaches 0

1.5k Views Asked by At

Can anyone help me with some algorithm for this problem?

We have a big number (19 digits) and, in a loop, we subtract one of the digits of that number from the number itself.

We continue to do this until the number reaches zero. We want to calculate the minimum number of subtraction that makes a given number reach zero.

The algorithm must respond fast, for a 19 digits number ($10^{18}$), within two seconds. As an example, providing input of 36 will give 7:

1. 36 - 6 = 30
2. 30 - 3 = 27
3. 27 - 7 = 20
4. 20 - 2 = 18
5. 18 - 8 = 10
6. 10 - 1 =  9
7.  9 - 9 =  0

Thank you.