Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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
share|cite|improve this question
The meaning of group seems to bear no connection to what is called group theory in mathematics, and distribution similarly doesn't seem to refer to statistical distributions. I've thus removed the two tags, and re-tagged as discrete-optimization, since that seemed like the best-matching tag. – fgp Oct 10 '12 at 15:11
thank you, I didn't the best matching tag as i'm not familiar with english mathematic words – Marcel Oct 11 '12 at 8:20

So you have 3 sets of values $g_1$, $g_2$, $g_3$ with sizes $n_1$, $n_2$, $n_3$. You want to pick $n$ of them by picking $p_i$ percent of the values in $g_i$, for $i = 1\ldots3$. Your $p_i$ thus need to fulfill $$ p_1n_1 + p_2n_2 + p_3n_3 = n $$ to guarantee that you'll pick $n$ elements overall.

Now, that equation obviously has lots of solutions, so you need additional requirements to have a unique solution. The requirement which produces your first solution is to require $p_1=p_2=p_3$. The solution is then $p_1=p_2=p_3=\frac{n}{n_1+n_2+n_3}$, as you have realized.

To extend this, you can assign a priority $q_i$ to each group, and require that $\frac{p_1}{q_1} = \frac{p_2}{q_2} = \frac{p_3}{q_3}$. In other words, you require that the percentages are the same after scaling them with the group's priority value. It follows that $p_2=p_1\frac{q_2}{q_1}$, $p_3=p_1\frac{q_3}{q_1}$ and thus $$ p_1\left(n_1 + n_2\frac{q_2}{q_1} + n_3\frac{q_3}{q_1}\right) = n $$ which yields the solution $$ \begin{eqnarray*} p_1 &=& \frac{nq_1}{n_1q_1 + n_2q_3 + n_3q_3} \\ p_2 &=& \frac{nq_2}{n_1q_1 + n_2q_3 + n_3q_3} \\ p_3 &=& \frac{nq_3}{n_1q_1 + n_2q_3 + n_3q_3} \end{eqnarray*} $$

If you set all priorities to the same value, you get the same percentages as in your initial solution. If you, for example, set $q_1$ twice are large as $q_2$ and $q_3$, each element from $g_1$ will have twice the chance of being picked, since $p_1$ will be twice as large as $p_2$ and $p_3$.

share|cite|improve this answer
Wauw, this looks awesome! I will get into this first thing tomorrow morning. Thanks a lot! – Marcel Oct 10 '12 at 17:49
hmm its not quite right. If i set the priority q1 higher than the q2 and q3. The solutions gives me more n1 items than g1 can give. – Marcel Oct 11 '12 at 7:36
hmm with a certain n value (higher than n1 and n2 and n3) The p (percent) is higher than 1! Wich means i get a bigger size (at the n with highest priority) than is possible – Marcel Oct 11 '12 at 7:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.