title: Find all numbers adding up to given number and range to use
body: <p>This is not homework, but a real world application I'm getting stumped on.</p>
  <p>The best way I can explain my problem is as follows:</p>
  <p>Imagine you have 3 pig pens <code>A</code>, <code>B</code>, and <code>C</code> and 10 pigs to be kept.
  The pens have restrictions in that each pen must contain at least 1 pig and <code>A.pigs &lt;= B.pigs &lt;= C.pigs</code>. List all the possible pen configurations.</p>
  <p>The REAL application can have anywhere between 1 and 7 pens, and anywhere between <code>numPens</code> and <code>numPens*30</code> pigs and no pen may contain more than 30 pigs. So essentially this translates into &quot;what numbers between 1 and 30 (repeats allowed) add up to X using Y numbers&quot;</p>
  <p>This could be solved simply with nested loops, but thats a terrible inefficient solution especially knowing the scope of the real application:</p>
  <pre><code>var cnt = 0
  val target = 10
  
  for (a in 1..30) {
      for (b in a..30) {
          val c = target - a - b
          cnt++
          if (a &lt;= b &amp;&amp; b &lt;= c &amp;&amp; a + b + c == target) {
              println(&quot;$a $b $c&quot;)
          }
      }
  }
  
  println(cnt)
  </code></pre>
  <p>output:</p>
  <pre><code>1 1 8
  1 2 7
  1 3 6
  1 4 5
  2 2 6
  2 3 5
  2 4 4
  3 3 4
  465
  </code></pre>
  <p>I'm sure there is a recursive solution to this problem. But I'm having trouble even finding the starting point for this one.</p>
  <p>It seems easy to start with an array of <code>[1, 1, 8]</code> where each index represents a pen. Just put all the pigs in 1 pen and move them around 1 at a time while following the constraints.</p>
  <p>Subtract from C, add to B as much as you can while keeping constraints gets us <code>[1, 2, 7], [1, 3, 6], [1, 4, 5]</code> but at that point I'm stuck code wise.</p>
  <p>What I have currently:</p>
  <pre><code>fun main(vararg args: String) {
      val list = arrayOf(1, 1, 8)
      list.forEach { print(&quot;$it\t&quot;) }
      println()
      rec(list, list.lastIndex - 1)
  }
  
  fun rec(list: Array&lt;Int&gt;, index: Int) {
      if (index == list.lastIndex || index &lt; 0) {
          return
      }
  
      while (list[index] + 1 &lt;= list[index + 1] - 1) {
          list[index]++
          list[index + 1]--
          list.forEach { print(&quot;$it\t&quot;) }
          println()
      }
  }
  </code></pre>
  <p>Obviously I need to call <code>rec</code> somewhere within itself and probably need some conditionals to call rec correctly (possibly adding more parameters to it). The question is where and how? Normally not bad with recursion, but this one is stumping me.</p>
  <p><strong>Solution:</strong>
  I got so hung up on the idea I needed recursion. This loop seems to work pretty well.</p>
  <pre><code>for (a in 1..30) {
      // we know a and c must be at least b, so cap range
      var b = a
      while(b &lt; min(target - a - b, 30)){
          val c = target - a - b
          cnt++
          if (a &lt;= b &amp;&amp; b &lt;= c &amp;&amp; a + b + c == target) {
              println(&quot;$a $b $c&quot;)
          }
          b++
      }
  }
  </code></pre>