Im not sure this is the right place to ask but it's a math problem so i think i'm at the right spot!
- list $L$ can hold $n$ items
- We have 3 groups $g_1$, $g_2$, $g_3$ each with their own $n$ items
List $L$ needs to be filled with the items from $g_1$, $g_2$ and $g_3$ but has limited space. So this means not all items can be in $L$. To distribute the number of $n$ items equally over the groups I'm doing the following.
- L(25)
- g1(30)
- g2(10)
- g3(10)
summed $g$ items = 30 + 10 + 10 = 50
total $L$ space = 25
multiplier = 25/50 = 0.5
items from $g_1$ in $L$ 30*0.5 = 15
items from $g_2$ in $L$ 10*0.5 = 5
items from $g_3$ in $L$ 10*0.5 = 5
This makes a total of 25 items distributed equally over the groups
But now I want to make $g_1$ items more important than $g_2$ (and $g_3$) so that not 50% of the items are distributed but say 60% (in this case 30*0.6). This means $g_3$ and $g_3$ get les items distributed.
How can i accomplish this?
[EDIT]
So far i have:
/* * [Javascript] * ni = total items in category * n = total items to distribute * qi = priority * pi = percentage of items to get from category * ci = calculated items to schow */ var n = 25; var n1 = 30; var n2 = 10; var n3 = 10; var q1 = 2; var q2 = 1; var q3 = 1; // Solution with equal percentages var pi = n / (n1 + n2 + n3); // Solution with priority percentages var p1 = (n * q1) / ((n1 * q1) + (n2 * q2) + (n3 * q3)); var p2 = (n * q2) / ((n1 * q1) + (n2 * q2) + (n3 * q3)); var p3 = (n * q3) / ((n1 * q1) + (n2 * q2) + (n3 * q3)); var c1 = n1 * p1; //get the number of decimal places and add them to the next calculated items to schow var c1decimalleft = c1 % 1; c1 -= c1decimalleft; var c2 = (n2 * p2) + c1decimalleft; //get the number of decimal places and add them to the next calculated items to schow var c2decimalleft = c2 % 1; c2 -= c2decimalleft; //use toFixed(0) to get an integer as sometimes c3 = ##.999999999 var c3 = ((n3 * p3) + c2decimalleft).toFixed(0);
wich gives
n : 25 n1: 30 n2: 10 n3: 10 q1: 2 q2: 1 q3: 1 pi: 0.5 p1: 0.625 p2: 0.3125 p3: 0.3125 c1: 18 c2: 3 c3: 4
This is a right solution.
But when i change $n$ into 42 or higher i get this:
n: 42 n1: 30 n2: 10 n3: 10 q1: 2 q2: 1 q3: 1 pi: 0.84 p1: 1.05 << higher than 1 p2: 0.525 p3: 0.525 c1: 31 << Not possible c2: 5 c3: 6