The crazy mathematician owns a wide collection of numbers, and therefore the space he has left is quite limited. To save some, he must fold his integers, but unfortunately he is really lazy. Your task, if you wish to help him, is to create a function / program that folds a given positive integer for our number maniac.
How to fold an integer?
If it is evenly divisible by the sum of its digits, divide it by the sum of its digits. If it doesn't meet that requirement, take its remainder when divided by the sum of its digits. Repeat the process until the result reaches
1. The folded integer is the number of operations you had to perform. Let's take an example (say
Get the sum of its digits:
1 + 7 + 8 + 2 = 18.
1782is evenly divisible by
18, so the next number is
1782 / 18 = 99.
99is not evenly divisible by
9 + 9 = 18, hence we take the remainder:
99 % 18 = 9.
9is obviously divisible by
9, so we divide it and obtain
The result is
3, because 3 operations were required in order to reach
Rules and Specs
Some integers might have the sum of digits equal to
1, such as
100. Your program doesn't need to handle such cases. That means, you will be guaranteed that the integer given as input doesn't have the sum of digits equal to
1, and no operation with the given integer will result in a number whose sum of digits is
1itself, which is the "target"). For example, you will never receive
The input will be a positive integer higher than
Default Loopholes apply.
You can take input and provide output by any standard mean.
Input -> Output 2 -> 1 5 -> 1 9 -> 1 18 -> 2 72 -> 2 152790 -> 2 152 -> 3 666 -> 3 777 -> 3 2010 -> 3 898786854 -> 4
Here is a program that lets you visualize the process and try more test cases.
This is code-golf, so the shortest code in each language (scored in bytes) wins!