Husk, 12 bytes
←1ドル¡Ṡ§|÷%oΣd
Explanation
←1ドル¡Ṡ§|÷%oΣd Implicit input, e.g. n=1782
Ṡ§|÷%oΣd This part defines the transformation.
oΣ Sum of
d digits: s=18
Ṡ % n mod s: 0
§| or (take this branch if last result was 0)
÷ n divided by s: 99
¡ Iterate the transformation: [1782,99,9,1,1,1,...
1ドル Index of 1 (1-based): 4
← Decrement: 3
Print implicitly.
Husk, 12 bytes
←1ドル¡Ṡ§|÷%oΣd
Explanation
←1ドル¡Ṡ§|÷%oΣd Implicit input, e.g. n=1782
Ṡ§|÷%oΣd This part defines the transformation.
oΣ Sum of
d digits: s=18
Ṡ % n mod s: 0
§| or (take this branch if last result was 0)
÷ n divided by s: 99
¡ Iterate the transformation: [1782,99,9,1,1,1,...
1ドル Index of 1 (1-based): 4
← Decrement: 3
Print implicitly.