title: How to loop over all possible vectors of certain length in lexicographic order?
body: <p>Let's say we have a vector of length 4, where every element can be a number from 0 to 9. For example: <code>&lt;1, 8, 0, 3&gt;</code></p>
  
  <p>Instead of simply looping over all 10^4 possible vectors, I want to loop in an specific order. So I want to start from <code>&lt;0, 0, 0, 0&gt;</code>, go to <code>&lt;1, 0, 0, 0&gt;</code>, then:</p>
  
  <p><code>&lt;2, 0, 0, 0&gt;, &lt;3, 0, 0, 0&gt;, ..., &lt;9, 0, 0, 0&gt;, &lt;0, 1, 0, 0&gt;</code> </p>
  
  <p>and so on (notice the order in the last two). I cannot think of a way to write this for variable vector length.</p>
  
  <p>Let's say we are in the ith iteration, having the <code>i</code>th vector in the lexicographic order I mentioned above. Having the <code>i</code>th vector is necessary for doing some process in the <code>(i+1)</code>th vector. This scheme saves compute over randomly looping over all possible vectors.</p>
  
  <p>I haven't really found a non-brute force and memory efficient way to solve this problem. Specially considering that I should be able to support variable vector lengths and different ranges of numbers for entries.</p>