title: Javascript - all lottery combinations generator via recursion
body: <p>I am trying to write an algorithm that generates all lottery combinations recursively.</p>
  
  <p>Here is a none recursive variation that can be used to get C(max,6). e.g. c(7,6)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let min = 1;
  let max = 7;
  let lotteryNum = 0;
  let outputString = [];
  for (let a = min; a &lt;= max - 5; a++) {
      for (let b = a + 1; b &lt;= max - 4; b++) {
          for (let c = b + 1; c &lt;= max - 3; c++) {
              for (let d = c + 1; d &lt;= max - 2; d++) {
                  for (let e = d + 1; e &lt;= max - 1; e++) {
                      for (let f = e + 1; f &lt;= max; f++) {
                          lotteryNum++
                          outputString.push(format([a, b, c, d, e, f]) +
                              " &lt;= " + lotteryNum + "\n");
                      }
                  }
              }
          }
      }
  }
  
  function format(n) {
      n.forEach((v, i) =&gt; {
          n[i] = v &lt; 10 ? '0' + v : '' + v;
      });
      return n.join(' ');
  }
  console.log(outputString.join(''));</code></pre>
  </div>
  </div>
  </p>
  
  <p>outputs</p>
  
  <pre><code>01 02 03 04 05 06 &lt;= 1
  01 02 03 04 05 07 &lt;= 2
  01 02 03 04 06 07 &lt;= 3
  01 02 03 05 06 07 &lt;= 4
  01 02 04 05 06 07 &lt;= 5
  01 03 04 05 06 07 &lt;= 6
  02 03 04 05 06 07 &lt;= 7
  </code></pre>
  
  <p>I tried writing a recursive algorithm so I could get C(max,length) as the above is hard-coded to C(max,6) and came up with the below, but I am not sure how to get the params needed to build the combination string as above.</p>
  
  <pre><code>function doRec(min, max, offset) {
      if (offset &gt;= 0) {
          for (let a = min; a &lt;= max - offset; a++) {
              doRec(a + 1, max, offset - 1);
          }
      } else {
          // how  to create outputString params?
      }
  }
  doRec(1, 7, 5);
  </code></pre>
  
  <p><strong>Bonus question,</strong> is there a straight forward way to mathematically convert a lottery combination to an integer and vice versa as opposed to using the brute force method above?</p>
  
  <p>e.g. from the output above</p>
  
  <pre><code>  01 02 03 04 05 06 &lt;=&gt; this is lottery number 1
    02 03 04 05 06 07 &lt;=&gt; this is lottery number 7
  
    so something like getLotteryNumber('02 03 04 05 06 07') returns 7 in this case.
  </code></pre>