12
0
Convert a number to a sum of digits
Not any sum: we need the shortest sum
Not any digits: you can use only digits of the number
Example
You will be given as input an integer n>0
Let's say n=27
. You have to express 27
as a sum, using only the digits [2,7]
, in the shortest way possible. You don't have to use all the digits of the given number!
So 27=2+2+2+7+7+7
. We then take those digits and count them: [2,2,2,7,7,7]
.
Final answer for n=27
is 6
One more example for n=195
in order to get the shortest sum we have to use the following digits:
[5,5,5,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]
and the answer is 23
The challenge
Given an integer n>0
, output the minimum number of digits (contained in the number) that sum up to this number
Test Cases
Input->Output
1->1
2->1
10->10
58->8
874->110
1259->142
12347->1765
123456->20576
3456789->384088
This is code-golf.Shortest answer in bytes wins!
Are there any numbers that cannot sum to themselves/will they be input? – Stephen – 2017-08-30T23:34:57.680
1@Stephen They all can! – None – 2017-08-30T23:40:10.790
7@Stephen Because every number can be expressed as d_0 + 10d_1 + 100 d_2, etc... – geokavel – 2017-08-31T00:04:07.447
Can we take the input as string, char-array or integer-array? – Kevin Cruijssen – 2017-08-31T07:32:51.837
1@KevinCruijssen String is ok. char-array or integer-array are not. – None – 2017-08-31T09:02:02.593
How did you pick
[5,5,5,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]
over[5,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]
, is either correct? – Magic Octopus Urn – 2017-08-31T14:59:01.197@MagicOctopusUrn Yes, they are both correct. We are only interested in the minimum number of digits. – None – 2017-08-31T16:53:12.950