Here is routine I just made.

The way it works is this:

Total Qty to distribute: 101

Sum of ratios: 10,2,6,7,4 = 29 (ratio total)

Calculate fractional ratios:

101*10/29 = 34,82759

101*2/29 = 6,96552

101*6/29 = 20,89655

101*7/29 = 24,37931

101*4/29 = 13,93103

This gives initially:

34,6,20,24,13 (sum 97)

Distribute the ramainders. The highest fraction decides where the remainders

goes:

35,7,21,24,14 (sum 101).

This procedure is hardcoded to 5 numbers, but it can easily be expanded to a

variable number.

I hope it helps you.

procedure TForm1.DistributeButtonClick(Sender: TObject);

var

i, j, TotalQty, TotalRatio: Integer;

Qty: array[1..5] of Integer;

Ratio: array[1..5] of Double;

MaxRatio: Double;

begin

TotalQty := StrToIntDef(TotalQtyEdit.Text, 0);

Ratio[1] := StrToIntDef(Ratio1Edit.Text, 0);

Ratio[2] := StrToIntDef(Ratio2Edit.Text, 0);

Ratio[3] := StrToIntDef(Ratio3Edit.Text, 0);

Ratio[4] := StrToIntDef(Ratio4Edit.Text, 0);

Ratio[5] := StrToIntDef(Ratio5Edit.Text, 0);

TotalRatio := 0;

for i := 1 to 5 do

TotalRatio := TotalRatio + Trunc(Ratio[i]);

if TotalRatio = 0 then

Exit;

for i := 1 to 5 do

Ratio[i] := TotalQty * Ratio[i] / TotalRatio;

for i := 1 to 5 do

begin

Qty[i] := Trunc(Ratio[i]);

Ratio[i] := Ratio[i] - Qty[i];

TotalQty := TotalQty - Qty[i];

end;

{distribute the remainders}

while TotalQty > 0 do

begin

j := 0;

MaxRatio := -1;

for i := 1 to 5 do

if Ratio[i] > MaxRatio then

begin

j := i;

MaxRatio := Ratio[i];

end;

Inc(Qty[j]);

Ratio[j] := 0;

Dec(TotalQty);

end;

Qty1Edit.Text := IntToStr(Qty[1]);

Qty2Edit.Text := IntToStr(Qty[2]);

Qty3Edit.Text := IntToStr(Qty[3]);

Qty4Edit.Text := IntToStr(Qty[4]);

Qty5Edit.Text := IntToStr(Qty[5]);

end;

Quote:

> Hi All,

> here is a bit of a mathematical problem I have and would like

> some assistance with.

> A qty e.g. 101 has to be distributed amongst 5 people

> in the ratio x1:x2:x3:x4:x5 e.g. 10:2:6:7:4 such that

> there are no remainders (from the original qty of 101)

> and no fractions i.e. only whole quantities (only integers)

> are acceptable.