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 1782
):
Get the sum of its digits:
1 + 7 + 8 + 2 = 18
.1782
is evenly divisible by18
, so the next number is1782 / 18 = 99
.99
is not evenly divisible by9 + 9 = 18
, hence we take the remainder:99 % 18 = 9
.9
is obviously divisible by9
, so we divide it and obtain1
.
The result is 3
, because 3 operations were required in order to reach 1
.
Rules and Specs
Some integers might have the sum of digits equal to
1
, such as10
or100
. 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 to1
, and no operation with the given integer will result in a number whose sum of digits is1
(except for1
itself, which is the "target"). For example, you will never receive10
or20
as input.The input will be a positive integer higher than
1
.Default Loopholes apply.
You can take input and provide output by any standard mean.
Test Cases
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!
8987868546
is a valid input, it will break your test tool, and also many (if not all) of the answers... \$\endgroup\$898786854
, not8987868546
(you have added a6
at the end) \$\endgroup\$8987868546
is not 1 (Rule 1 met) and8987868546
is a positive integer higher than 1 (Rule 2 met). \$\endgroup\$