Reputation: 1451
I have to allocate items for customers. In the allocation algorithm all customers should have a specific minimum, before I allocate the remaining items.
Some of these items are pre-picked. For these pre-picked items is at least one container quantity given. Sometimes two.
I need an efficient algorithm to find the minimum value which is above the given minimum value per customer and can be divided by at least one container quantity without remainder.
Here some minimalistic code for better understanding:
int containerQty1;
int? containerQty2; // Can be null
int customerMinimum;
int minimumToGet;
In this example, minimumToGet
should have a higher value than customerMinimum
, but has to be divided without remainder by containerQty1
or containerQty2
.
Upvotes: 1
Views: 887
Reputation: 841
static int AmountToGet(int containerQty, int custMin) {
return custMin + containerQty - (custMin % containerQty);
}
Now with that:
var amountToGet = AmountToGet(containerQty1, minimumToGet);
if (containerQty2 != null) {
amountToGet = Math.Min(amountToGet, AmountToGet((int) containerQty2, minimumToGet));
}
Upvotes: 1
Reputation: 726699
Given two numbers, M
and D
, you can construct the smallest number X
above M
such that D
divides X
by first adding D
to M
, and then subtracting the remainder of the division of the sum by D
, i.e.
int X = (M+D) - ((M+D) % D);
Example: M = 200
, D = 13
. We are looking for a smallest multiple of 13 above 200.
int X = (200+13) - ((200+13) % 13) = 213 - 5 = 208;
Upvotes: 3
Reputation: 9085
Find the remainders after dividing the customer minimum by the two containers. Add the smallest (container less remainder).
E.g., customer min = 20, q1 = 7, q2 = 11.
20 + (7 - 20%7) = 21
20 + (11 - 20%11) = 22.
So in this case you'd pick the 7 container and 21.
Upvotes: 1