Maths-Distribution 
Author Message
 Maths-Distribution

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.

Solution can be either
1)
The basic rule of distribution has to be fulfilled
i.e. (using Ratios) (The easy part <g>)
before any other rule/rules are applied to the remainders
which is of course open. The catch is, integers only. There
can be zeros i.e. a person can receive nothing.

or
2)
Any formula but the ratios are a guide for distributing and
only integer results accepted and there should be no remainders.

Are there any websites or references that can help me solve this.
Is there anyone, who would not mind this mind tasking problem.

All suggestions welcome.

Thanks in advance.

--
Arnold Johnson



Wed, 18 Jun 1902 08:00:00 GMT  
 Maths-Distribution
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.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Weird maths (sqrt)

2. Programming For Maths

3. Floating Point Maths under BPW

4. MATHS FUNCTION PARSER

5. A few maths questions(groan).

6. Catching maths errors (eg divide by zero)

7. Maths on NULL integer fields in Interbase

8. Distribution of BDE

9. Delphi & MSAccess runtime distribution

10. ADO Distribution

11. BDE Distribution Problem

12. Fibonacci distribution ???

 

 
Powered by phpBB® Forum Software