- #1

Chao Li

- 1

- 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);
}
}
}
}
```

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?