Need a formula to know how many times 2 different numbers fit into a 3rd number.

38 Views Asked by At

I am creating a chairs seating diagram and need a fast way to be able to input my 2 chair widths of 20", and 22" into a total width and for it to tell me the maximum amount of chairs that will fit in that row. I also need the output to be a whole number for both sizes.

For example I have a length of 160". In this example, I can actually get the most amount of chairs by using only (8) 20" chairs. It equaling a perfect width of 160" with no remaining.

However if I have a number of 144". Then the best config is (2) 22" chairs, and (5) 20" chairs.

The formula can have left over numbers such as the width being 145". Then the best config is the same as 144" (2) 22" chairs and (5) 20" chairs. I am not concerned with that remaining 1" left over from 145" since that total is 144" the extra 1" is of no concern.

2

There are 2 best solutions below

2
On BEST ANSWER

Based on the examples you gave, I'm assuming, even though you didn't state it, that you want to use up as much of the space as possible. Otherwise, just use only 20" chairs. Or maybe it's "I'd prefer 22" chairs, but not if it means having fewer chairs". Either way, here's an easy algorithm:

First, figure out how many 20" chairs you could fit.

Then, figure out how much space is left over.

For each 2" in the left over space, replace a 20" chair with a 22" chair.

2
On

This is a special case of the knapsack problem. Because you care only about the total number of chairs, you can ignore the larger chair and just use the floor function.

  • For your first example, the maximum is $\lfloor 160 / \min(20,22) \rfloor = 8$.
  • For your second example, the maximum is $\lfloor 144 / \min(20,22) \rfloor = 7$.
  • For your third example, the maximum is $\lfloor 145 / \min(20,22) \rfloor = 7$.