6
A woman works at a lottery ball factory. She's instructed to open up a lottery ball creation kit composed of 1 red rubber ball and n strips of digits from 0 through 9.
She's instructed to do the following:
- Starting with 1, write the current number on the ball using the digits available on the n strips.
- Put unused digits into a bowl where they can be used later for when she runs out of available digits in step 1.
- Open up another lottery ball creation kit and continue to step 1 for the next number.
The question is, at what number will she arrive at before she runs out of digits to write the number? Edited: Given n where n is the number of digit strips from 0 through 9 provided in each lottery ball creation kit, write a program that can solve this (not trivially) in the least amount of time.
Edit: Additional notes - 6 and 9 are different numbers. Assume there are no limits to the number of digits that can be pasted on the ball.
Hint: If she counted in base 2 rather than base 10, how would that change the result and would that be easier to calculate? What could you derive from solutions in base 2?
2Are 6 and 9 considered to be different ? – Paul R – 2011-06-29T16:25:45.587
1@Paul R, irrelevant. It's easy to prove that the first digit which runs out is 1. – Peter Taylor – 2011-06-29T17:01:59.687
1I think we need some more detail. The way I'm reading the question, she'll run out of digits on the strips at ball #111 (since there are only two 1's) and then need to go into the bowl. Are we talking about running out of digits in the bowl? That too should have a relatively simple solution, and I'm not sure it's interesting to see which language can be compiled most efficiently since there's not much room for algorithmic differences in a simple increment+check problem. – Matthew Read – 2011-06-29T17:05:04.770
@Matthew, she can use the 1s from the bowl for 111. The solution will be somewhere just short of 10^10, so increment and check won't be the fastest approach. – Peter Taylor – 2011-06-29T17:10:19.980
How many digits can she fit on one ball ? – Paul R – 2011-06-29T17:25:48.787
I think the shortest answer for this question will be something like
print X
. – Alexandru – 2011-06-29T17:27:59.830@Alexandru: I think that's already covered by the "(not trivially)" clause. – Paul R – 2011-06-29T17:31:19.843
2@Peter I guess I'm confused about how you can do anything intelligent beyond increment and check without making it trivial. It seems kind of silly to take advantage of various properties of the problem while ignoring the obvious one, that it has a direct and fixed solution. I don't think this is a good question. – Matthew Read – 2011-06-29T17:46:39.993
@matthew you can iterate through it through the magnitude; the digits you need for 1 through 1e(X+1) is equal to 10 times the digits you need for 1 through 1eX. plus 1e(X-1) for the ending digits (with some caveats though) – ratchet freak – 2011-06-29T20:45:31.157
2@Matthew, it should be possible to do something binary-searchy. (If anyone can be bothered to check which Project Euler problem is very similar to this question I might check my solution). But I agree that fixed mathematical problems are bad questions. Perhaps the problem should be fixed by making the number of strips in each kit an input variable? – Peter Taylor – 2011-06-29T20:53:18.237
2All good points. Making relevant corrections to the question. – Neil – 2011-06-30T12:03:26.410
@Matthew: She places 1 on the ball and places the rest of that strip (0 and 2 through 9) in a bowl along with any additional strips (0 through 9) every number. For example for n = 2, when she finishes with number 2, she's got 3 ones, 3 twos, and 4 of numbers three through nine at her disposal (she's gone through 4 digit strips total only to use a 1 digit and a 2 digit). – Neil – 2011-06-30T12:35:08.060
1I'm assuming the result will be of the order of 10^21, i.e. a 21 digit integer, give or take a digit ? In which case 64 bit ints aren't going to be much use ? – Paul R – 2011-06-30T20:24:31.873
@Paul, seems like a pretty good assumption with hindsight for n=2. Or 10^31 for n=3... – asoundmove – 2011-07-01T04:29:57.487
I think static value types are out of the question, in fact. – Neil – 2011-07-01T10:10:29.713
1
Found it: Project Euler problem 156 is related (but different).
– Peter Taylor – 2011-07-01T12:26:23.763@Peter Taylor: how can it be proven that the first digit to run out is 1? – Lowjacker – 2011-07-01T16:12:46.297
@Lowjacker: I don't know about a formal proof, but it's fairly self-evident that the digits 1-9 all get consumed at the same aggregate rate over any given order of magnitude, but the 1s will always get used first, e.g. consider the ranges 1..9, 10..99, 100..999, etc. – Paul R – 2011-07-01T16:22:30.983
1@Lowjacker, let f(d, n) be the number of digits d used up by the first n balls. Then for d in [2,9], f(d-1, n) >= f(d, n). Proof: consider the set of (number, digit position) pairs which contribute towards f(d, n). Replacing the digit d at that position in that number you get a smaller number, so if the first number is <= n, the one with the digit replaced also is, and that contributes to f(d-1, n). QED. 1 is a special case because leading 0s are ignored. – Peter Taylor – 2011-07-01T18:58:39.683
@Peter Taylor I thought about your argument and don't come around one point. I think you'll have to add another argument. If I understand correctly, for
f(2,21)
the numbers2
,12
,20
and21
contribute. Replacing digit2
by1
we get numbers1
,11
,10
and11
. Note that11
is actually a duplicate but must not be counted twice forf(1,21)
! – Howard – 2011-07-02T15:26:47.163@Howard: incorrect.
11
must be counted twice forf(1, 21)
because it contains the digit1
twice. – Peter Taylor – 2011-07-02T19:20:39.323@Peter Taylor. yes, it counts 2 ones. But that's not what I meant. You would take it a third time if you think of
f(2,22)
because then also22
would be mapped to11
. – Howard – 2011-07-02T19:48:54.093@Howard, no:
22
would be mapped to12
and21
. I was quite careful to set up my replacements to only replace one digit at a time. – Peter Taylor – 2011-07-03T19:17:14.447@Peter now I understand your point. thank you for the clarification. – Howard – 2011-07-03T20:15:48.023
@Neil: you say "Answer for n=1 is 199991 since technically 199990 is the last number she could make" yet the question says "what number will she arrive at before she runs out of digits". My understanding is that if she runs out of digits writing 199991, then the answer is the number before, hence 199990. – AtnNn – 2011-07-05T00:57:25.933
@atnnn You're right of course, though if you gave me 199991, I'd consider it the right answer anyway. – Neil – 2011-07-05T07:28:31.327