20
1
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!
Inspired by this challenge, although it might not seem related at first. – Mr. Xcoder – 2017-08-16T09:02:44.307
3
This will work as a stopgap solution, but in the long term, the mathematician should really consider purchasing one of Hilbert's Hotels. You can always find some unused room in one of those.
– Ray – 2017-08-16T22:13:30.640while
8987868546
is a valid input, it will break your test tool, and also many (if not all) of the answers... – Mischa – 2017-08-17T06:55:34.913@MischaBehrend Your example is not a valid input. I think you miscopied my last test case. The valid input was
898786854
, not8987868546
(you have added a6
at the end) – Mr. Xcoder – 2017-08-17T07:10:19.753nvm... should read the whole first rule ... leaving this here so you know why i thought it is valid: it was not a mistake... I changed it intentional to test these scripts... and reading the rules it is a valid input. The sum of all the digits in
8987868546
is not 1 (Rule 1 met) and8987868546
is a positive integer higher than 1 (Rule 2 met). – Mischa – 2017-08-17T07:16:05.867@MischaBehrend *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 1* - I believe this is the part you missed. – Mr. Xcoder – 2017-08-17T07:42:20.290
yes, I exactly missed that part. – Mischa – 2017-08-17T07:44:37.487