Finding the minimum number of boxes to pack products

Click For Summary
SUMMARY

The discussion focuses on optimizing the packing of products using three box sizes: small (holds 3 items), medium (holds 5 items), and large (holds 9 items). The goal is to determine the minimum number of boxes required to pack exactly N items, represented mathematically by the equation 3x + 5y + 9z = N, where x, y, and z are the counts of small, medium, and large boxes, respectively. The initial solution employs nested loops to explore all combinations, but concerns about efficiency with larger numbers lead to a proposed mathematical approach that prioritizes using large boxes first and calculating residual items for optimal packing.

PREREQUISITES
  • Understanding of basic algebra and equations
  • Familiarity with programming concepts, particularly loops and conditionals
  • Knowledge of modular arithmetic for calculating remainders
  • Experience with optimization techniques in algorithm design
NEXT STEPS
  • Research mathematical optimization techniques for combinatorial problems
  • Learn about dynamic programming approaches to minimize box usage
  • Explore greedy algorithms for packing problems
  • Investigate the use of integer programming for solving similar optimization challenges
USEFUL FOR

Software developers, algorithm designers, and operations researchers interested in optimization problems, particularly in logistics and resource allocation.

Chao Li
Messages
1
Reaction score
0

Homework Statement


This was a coding challenge, and I've already completed it. But I feel there must be a better solution to my approach at the moment. Especially maybe there's a mathematical approach.

You have 3 sizes of boxes, small, medium, large:
small: holds 3 items
medium: holds 5 items
large: holds 9 items

If some one wants N items, how many x small, y medium and z large boxes to you need to pack that exact number, while ensuring that you use the minimum number of boxes?

I've solved this using loops, but I'm just wondering if anyone have a mathematical solution for this?

Homework Equations


3x + 5y + 9z = N
solve for x, y, z such that x + y + z is a minimum

The Attempt at a Solution


C:
int xlim = number/smallPackSize;
int ylim = number/mediumPackSize;
int zlim = number/largePackSize;

// loop thru all possibility
for (int x = 0; x <= xlim; x++) {
    for (int y = 0; y <= ylim; y++) {
        for (int z = 0; z <= zlim; z++) {
            int totalProduct = smallPackSize*x + mediumPackSize*y + largePackSize*z;

            // if total product number equals what we requested, save this combination.
            if (totalProduct == number && totalProduct != 0) {
                smallPackNo.add(x);
                mediumPackNo.add(y);
                largePackNo.add(z);

                totalPackNo.add(x + y + z);
            }
        }
    }
}
<Moderator's note: edited to add code tags. Please use them when posting code.>
Then just find the x,y,z combination that is a minimum from the lists.

This solution works. But I am concerned that it will be slow with large numbers. Does anyone have a more efficient method?
 
Physics news on Phys.org
I think for a large number ##N## one can say use as much large sized packages as possible. So maybe it is faster to

1) calculate the number of large packages needed (## N/9 = ... ##) and skip the digits after the dot
2) calculate the residual items (## N\;mod\;9 = x ##), where ##x## only can be a number beyween 1 and 8 (if it is 0, the task is solve)
3) and then find rules depending on ##x## (e.g. if ##x = 8## then use on additional medium sized and one additional small sized package or if ##x = 1##, then decrease the number of large sized packages by one and add two medium sized packages instead, ...)
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K