title: Find fractions with common denominator for parcel areas , sum has to stay the same
body: <p>Lets say i have a parcel (a piece of land) that is 8027 square meters and i have more than one owner, but they don't own same amount of land.</p>
  <p>Owner A has: 1003.5m2
  Owner B has: 1003.5m2
  Owner C has 1337m2
  Owner D has 1338m2
  Owner E has 1338m2
  Owner F has 2007m2</p>
  <p>As output i want to get whole numbers fraction with lowest possible common denominator, etc</p>
  <p>1/8
  1/8
  ..
  ..
  ..</p>
  <p>i know it is very unlikely it will be this small...</p>
  <p>but when you divide fractions difference shouldn't exceed more than +/- 1m2 per owner</p>
  <p>meaning if owner A has 1003.5m2 in beginning, and i got 1/8 8027/8=1003.35 that is OK, BUT, unfortunately big BUT! total SUM of all owners has to stay the same at the end :(</p>
  <p>For now i was able to get lowest common denominator for owners individually and i'm not even sure what is next step in getting wanted result...</p>
  <p>Any help is much appreciated!</p>
  <pre><code>gcd = function (a, b) {
      if (b &lt; 0.0000001)
          return a;                         // Since there is a limited precision we need to limit the value.
      return gcd(b, Math.floor(a % b));     // Discard any fractions due to limitations in precision.
  };
  
  var total_area = 8027;
  var area_parts = [1003.5, 1003.5, 1337, 1338, 1338, 2007];
  
  ResaultArr = Array();
  
  for (let i = 0; i &lt; area_parts.length; i++) {
      var decimal_fraction = area_parts[i] / total_area;
      var denominator = Math.pow(10, 4);    
      var numerator = decimal_fraction * denominator;
      var divisor = gcd(numerator, denominator);
  
      numerator /= divisor;
      denominator /= divisor;
  
      var fraction = Math.floor(numerator) + '/' + Math.floor(denominator);
      var decimal_fraction_after = Math.floor(numerator) / Math.floor(denominator);
      var area_difference = area_parts[i] - (total_area * decimal_fraction_after);
      var area_part_after = total_area * decimal_fraction_after;
  
      ResaultArr[i] = {
          area_difference: area_difference,
          fraction: fraction,
          decimal_fraction: decimal_fraction,
          decimal_fraction_after: decimal_fraction_after,
          area_part: area_parts[i],
          area_part_after: area_part_after,
          total_area: total_area
      };
  }
  
  console.log(ResaultArr);
  </code></pre>