title: Dividing a number into groups of numbers
body: <p>I have an algorithm that generates a range of numbers. The parameters are <code>$min</code>, <code>$range</code>. If I give the values:</p>
  <pre><code>$min = 4
  $range = 1
  </code></pre>
  <p>The algorithm generates the numbers, that can be grouped in the minimum to the maximum range. Say, for the above inputs, the algorithm generates the numbers starting from <code>$min</code>, <code>$min + $range</code>, multiplied by the increments:</p>
  <pre><code>Iteration n: range($min x n to ($min + 1) x n)
  Iteration 1: 4, 5            // range (4, 5)
  Iteration 2: 8, 9, 10        // range (8, 10) 
  Iteration 3: 12, 13, 14, 15  // range (12, 15) 
  // and so on!
  </code></pre>
  <p>So, if I give the input number as <code>13</code>, we can understand that it can be obtained by any of the ways:</p>
  <pre><code>1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
  2 + 2 + 2 + 2 + 2 + 2 + 1
  3 + 3 + 3 + 3 + 1
  4 + 4 + 4 + 1
  5 + 5 + 3
  6 + 6 + 1
  7 + 6
  8 + 5
  </code></pre>
  <p>And so on. But based on the inputs, we have numbers only in the range <code>4</code> and <code>5</code>. So, I should get the answer:</p>
  <pre><code>4 + 4 + 5
  </code></pre>
  <p>If I go by the above method, of starting with bigger numbers, it will iterate by:</p>
  <pre><code>5 + 5 + 3     // 3 is Invalid
  4 + 4 + 4 + 1 // 1 is Invalid
  </code></pre>
  <p>How to make the algorithm understand that the common match or something like Highest Common Factor or Greatest Common Divider concept, we need to get only the following result:</p>
  <pre><code>4 + 4 + 5
  </code></pre>
  <p>I am stuck with this, on how to proceed. The above explanation is the implementation of the algorithm, but I have no clue how to implement it.</p>
  <p>One of the try that I have is, making a mix of all the numbers and try to flag off each bit. So, at the first attempt, I am taking the smallest number to match it with the least:</p>
  <pre><code>4 + 4 + 4
  </code></pre>
  <p>As, <code>4 + 4 + 4 + 4</code> exceeds <code>13</code>. Now, am taking the next guy:</p>
  <pre><code>5 + 5 + 5
  </code></pre>
  <p>And keeping the two sets of inputs, I am reducing the number one by one:</p>
  <pre><code>Try 0: 5 + 5 + 5 // Larger
  Try 1: 5 + 5 + 4 // Larger
  Try 2: 5 + 4 + 4 // Success
  </code></pre>
  <hr />
  <h3>Update #1</h3>
  <p>Current implementation is:</p>
  <pre><code>function getNums($k, $range, $num)
  {
      $q = floor($num / $k);
      $r = $num % $k;
      echo str_repeat($q . &quot;, &quot;, $k - $r), str_repeat(($q+1) . &quot;, &quot;, $r);
  }
  </code></pre>
  <p>The output is bizarre:</p>
  <pre><code>getNums(4, 1, 13); // 3, 3, 3, 4 // Expecting 4, 4, 5
  getNums(4, 1, 18); // 4, 4, 5, 5
  </code></pre>