The integers from 1 to 1000 (inclusive) are to be placed into boxes such that no box contains a number that is a multiple of another number in the same box.
What is the minimum number of boxes needed?
As an explicit example, the integers from 1 to 10 can be placed into 4 boxes while satisfying the condition:
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
As always, with finding a max/min, we need to 1) show that no higher/lower number works and 2) show that this number works.
This solution provides a clear explanation of why 1) we need at least 10 boxes, and 2) why 10 boxes is sufficient.
Nice question and solution. The way you have filled the boxes is the most elegant, but I solved it using a different arrangement. In box n , 1 ≤ n ≤ 1 0 , I placed all the positive integers ≤ 1 0 0 0 which have n − 1 prime factors, (inclusive of multiplicity). So in box 1 I placed 1 alone, in box 2 I placed all the primes, in box 3 I placed 4 , 6 , 9 , 1 0 , 1 4 , . . . . , in box 4 I placed 8 , 1 2 , 1 8 , 2 0 , . . . , and so on until in box 1 0 I placed just 2 9 = 5 1 2 and 2 8 × 3 = 7 6 8 .
Which makes me wonder: how many different ways can we arrange 1 to 1 0 0 0 in 1 0 boxes that satisfy the conditions of your question?
Log in to reply
Thank you for the feedback. And I will certainly think about your new problem.
I did it like that too. Felt more natural to me.
Log in to reply
I agree, Brian, I think you should post your comment as a solution because it's a very nice alternative interpretation!
(And please post that follow up problem as well)
Log in to reply
@Pi Han Goh – Looks like Jesse Nieminen has already posted that solution partition. As for the follow-up question, I first have to figure out what the answer is. If I can't, I'll post it as a discussion topic.
This is really a very elegant partition!
Clearly all of 2 0 , 2 1 , … , 2 9 need their own boxes and thus 1 0 is the minimum.
However, by having all numbers with the same number of prime factors (not necessarily distinct) in the same box, we cannot have any of them dividing each other. We can achieve this using exactly 1 0 boxes, since all numbers between 1 and 1 0 0 0 can have at most 9 prime factors.
Hence, the answer is 1 0 .
There is no need to consider prime factors. For box number n we simply pick all numbers 2 n − 1 , … , 2 n − 1 . Since 2 n − 1 2 n − 1 < 2 , we are sure that none of them are multiples of each other.
Log in to reply
You are right! This really is an elegant way to categorise all numbers.
This is the simplest and cleanest proof I have seen and with a direct method to compute the box number without factoriing. That beats the program code "solutions" for both being a proof and not a good guess, and efficiency of code that might be written. I did it the hard way with the prime multiples where each box holds all factorizations in range with the same number of factors. The number of boxes is the least n such that 2^n < N e.g. N = 1000 that can be calculated as CEILING(Log2(N)) + 1. Change the list to 2...N saves a whole box. As a math student (even with two math degrees I do not call myself a "mathematician", that honor I reserve for those who discover something new, and not just new to them) and experienced programmer I am a bit ashamed of not hitting the binary break out idea right off.
Oh, that's a nice way to ensure that the non-divisibility condition is met.
I solved this one by programming it. Here is the solution in Java.
http://tinyurl.com/PiggBoxes
I looked through the code. Correct me if I'm wrong, but what it seems to do is use the greedy algorithm approach of "check existing boxes. If none works, create a new box". This is how the explicit example of 10 was derived.
However, the greedy algorithm is only locally optimal (if we want to add 1 element to the existing boxes, then it will find the best way), but not necessarily globally optimal (maybe we want to create a new box for the element 3, even though we could place it along with 2, so that in a later stage we can place the element 9 in the box).
As such, we've found that 10 boxes would work, but do not yet know if it's the minimum . Using programming, how can we close this gap and show that no other smaller number of boxes would work?
Log in to reply
Your argument seems to be interesting about scope.
Can you please tell how to resolve it ? Thanks!
Log in to reply
What do you mean by "scope"?
It is currently my belief that a pure coding approach cannot resolve the minimum issue, other than brute force checking. IE Show that for every arrangement of 1000 (or 512) numbers into 9 boxes, there are 2 that are a multiple of each other.
See my conversation with Greg in his solution.
You did not solve it. Assuming the code is bug free, you discovered an upper bound, without any proof it is a minimum.
The brute force method for this question is also very simple and fast. It is intuitively obvious that for the number of boxes to be minimum , it is more desirable to group largest possible numbers together as large numbers which are "close" are very less likely to be multiples of each other.
Hence we start of from 1000 and descend until we find a number where we disrupt the rule. Now for 1000 , we can group till 501. As 500x2 =1000. Similarly for 500 , we can go on till 251.
When n is even ---> we can group till n/2 +1
Where n is odd ---> we can group till (n+1)/2
Using this rule , we can easily count the groups within seconds :-
(1)...1000-501
(2)...500-251
(3)...250-126
(4)...125-63
(5)...62-32
(6)...31-16
(7)...15-8
(8)...7-4
(9)...3,2
(10)...1
It's minimum considering the fact that we are grouping maximum possible elements in each set. It is important to note that we are not randomly grouping the elements but orderly descending from the highest number. Thus we are adding another box only when we strictly have to.
•There is no scope of movement of an element from a smaller set to a bigger set as it immediately violates the rule. • While moving a number from a bigger set to a smaller set does not change the number of sets (boxes) , thus serving no purpose whatsoever.
Therefore , the number of boxes in our arrangement is the least amount possible.
How do we know that this is really the minimum?
Log in to reply
It's minimum considering the fact that we are grouping maximum possible elements in each set. It is important to note that we are not randomly grouping the elements but orderly descending from the highest number. Thus we are adding another box only when we strictly have to. There is no scope of movement of an element from a smaller set to a bigger set as it immediately violates the rule. While moving a number from a bigger set to a smaller set does not change the number of sets (boxes) , thus serving no purpose whatsoever. Therefore , the number of boxes in our arrangement is the least amount possible.
Log in to reply
You are attempting to prove a greedy algorithm and the main idea is correct. Your reasoning could be made stronger if you justify that your solution is at least as good as the optimal solution, rather than because it couldn't be improved.
Another way to show that 10 is a minimum is to establish is: 1. Show that we must have at least 10 boxes. 2. A solution using 10 boxes is attainable.
Log in to reply
@Christopher Boo – Thank you for replying to my solution.The proof using the powers of 2 has already been mentioned so I wanted to try something different. Isnt Justifying that the answer can't be improved further same as justifying that our answer is just as good as the optimal solution? I may be missing something here because I can't seem to find a way to add anything to my solution. Can you please help me out in refining the solution more? Thanks.
Log in to reply
Isnt Justifying that the answer can't be improved further same as justifying that our answer is just as good as the optimal solution?
Unfortunately, no. You still need to justify why the solution that you have found is optimal. Without that, all your solution says is "I've found this method, but I don't know if there's a better method."
@Bhuvanesh Sridharan – Your solution is to show that you utilise the first box, and then the second, third etc. How to be sure that maybe you can choose to not utilise the first box but you can perform better for the second or third?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
You are using the greedy algorithm approach of "check existing boxes. If none works, create a new box". This is how the explicit example of 10 was derived.
However, the greedy algorithm is only locally optimal (if we want to add 1 element to the existing boxes, then it will find the best way), but not necessarily globally optimal (maybe we want to create a new box for the element 3, even though we could place it along with 2, so that in a later stage we can place the element 9 in the box).
As such, we've found that 10 boxes would work, but do not yet know if it's the minimum . Using programming, how can we close this gap and show that no other smaller number of boxes would work? It seems to me that the only way is to "Test all possible arrangement of up to 9 boxes and show that it doesn't work". Any other thoughts?
Hi Calvin,
I see what you mean.
I can edit the code to attempt to place the next integer into a bin that has the least elements ... still it's not a full solution, but it may provide a new arrangement of those integers that satisfies the criteria.
As for a true minimization, I'll think some more. Greg
OK - I rewrote my script. Interestingly, I can pack all the integers from 1 to 1000 into 10 rows by 113 columns when I search for the least full row first. This is in contrast to the simple 'search from the top approach', which uses 10 rows by 299 columns.
As for the solution, as others have stated, since 2^n has at least one of the following (1,2,4,8,16,32, ..., 2^n-1) as a factor, each new 2^n will require a new box (or a new row in my program.)
As for testing the minimization, consider this: - the integers 512, 256, 128, 64, 32, 16, 8, 4, 2, and 1 already each exist in different boxes. - when 1024 is to be placed into a box, it is evenly divisible by each of the above integers, which are already in a box. - The result is that there exists no solution, other than to place 1024 into a new box.
Log in to reply
As for testing the minimization, consider this: - the integers 512, 256, 128, 64, 32, 16, 8, 4, 2, and 1 already each exist in different boxes. - when 1024 is to be placed into a box, it is evenly divisible by each of the above integers, which are already in a box.
Yes, this indeed proves that we require at least 10 boxes.
However, without this reasoning, it is not clear why the code must create optimal partitioning for the integers. While we are filling in the box with the least number of partitions at every step, it is not necessary that this will give the optimal solution overall.
I don't know if you have actually updated your code in the solution. I ran this in octave and it still has 299 columns.
Log in to reply
I didn't post the update, but what I did was that at line 42, I added a function to sort the matrix. I can post it if you want, but I don't want to enrage anyone. (See above). With the sorted matrix, it packs it into the 113 columns. It's not a solution, but I did enjoy writing the short program.
Log in to reply
@Greg d'Entremont – I plan to post this as a new problem, although I am not sure of the answer.
Great! Thanks for analyzing the CS aspect of the problem :)
I am appalled at the posting of computer programs as "solutions" to this problem without any supporting proof of the correctness of the code. Some are unclear about what constitutes proof in mathematics of which the computer science of algorithms is a subset. There are several good concise mathematical proofs posted that not only solve this puzzle for [1..1000] but solve it for [1..N] at no additional effort. Some also provide a simple explicit mapping of numbers to boxes that can be programmed much more efficiently than what the code "solutions" provide, and with the absolute assurance that the solution is correct.
Thinking comes before coding. Coding alone is not proof, it is computer assisted hand waving. A person armed with the insight shown by Vreugdenhil would not only have properly found the answer to the quiz, but found it on a basis good for any finite list of consecutive positive integers. If interested this person would also have coded and tested a box mapping program before a person writing the brute force box stuffing algorithm, that is not a proof, had managed a clean compile.
The general solution of the minimum question can be expressed as CEILING(log2(N)) + 1 based on the proofs given. In words, the minimum number of boxes is the least n such that N < 2^N. (the edge case is N = 2^m, when one more box is needed compared to 2^m - 1. This requires the strict inequality, and the "+1" on the expression above.)
The most simple and effective was given elsewhere on the thread by Arjen Vreugdenhil. He observed that no integers in the interval [2^(n-1), 2^n - 1] (n> 1) can divide each other, thus these intervals define a suitable box and its contents. The interval [1,1000] can be covered by 10 of these disjoint intervals with with n = 1..10 (with a few left over from 1001 to 1023). This puts every integer into one of 10 distinct boxes and establishes that none in a box is a multiple of another. That each of 10 powers of 2 starting with 1 = 2^0 to 512 = 2^9 must have a distinct box establishes that this 10 is also the minimum. Box number for each integer, x, is given directly by CEILING(log2(x)) + 1. So this one works well as a proof and is very constructive when the actual mapping can be computed trivially.
Before answering at the website I worked it the hard way with boxes defined as groups of integers having the same number of prime factors. It is more work as a proof, and much more work to code to compute the box numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
replace variable max_int (currently 1000) with another value within recursion limit to recalculate. Max value I could calculate on my machine = 3880
Problem Loading...
Note Loading...
Set Loading...
One can easily see that a solution with 10 boxes is possible with the following illustration:
...
Likewise, it is not difficult to understand why 10 is the optimal number of boxes, for every power of 2 greater than 1 requires a new box as it is obviously a multiple of every smaller power of 2. Hence, 10 boxes are necessary because there are 10 different powers of 2 between 1 and 1000.