Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simple way to break it down is to sum the counts as parts. (For my examples, I used 1-based index)</p> <p>Let's say you're given (fewer digits, but same principle) the tuple ( 2, 3, 4 ). Its position is simply the sum of:</p> <ul> <li>( 1, x, y ) - all the numbers that fit into this</li> <li>( 2, 2, x )</li> <li>( 2, 3, x ) - where x &lt; 4</li> </ul> <p>and you can figure this out iteratively.</p> <p>Now, for D = 3 digits, and K items, you can draw out the pattern and see how it grows:</p> <pre><code>K = 1 1 1 1 K = 2 1 1 1 1 1 2 1 2 2 2 2 2 K = 3 1 1 1 1 1 2 1 1 3 1 2 2 1 2 3 1 3 3 2 2 2 2 2 3 2 3 3 3 3 3 </code></pre> <p>for each iteration, what you're doing is actually taking the previous groupings (include an empty grouping) and adding an additional number -- as in the triangular number sequence. You can even think of this recursively as you increase the first digit -- in the above example of D = 3, K = 3, you can re-map the symbols of the stuff that doesn't start with "1", and thus they do not contain any "1"s - D is still 3, but K is now 2:</p> <pre><code>K = 3 (ignoring 1's) 2 2 2 2 2 3 2 3 3 3 3 3 </code></pre> <p>becomes:</p> <pre><code>K = 2 1 1 1 1 1 2 1 2 2 2 2 2 </code></pre> <p>This is how you would add to K. (For D = 3, notice that they are triangular numbers.)</p> <p>How about adding a digit? Well, for K = 3, D = 3, you can imagine, given this:</p> <pre><code>K = 3 1 1 1 1 1 2 1 1 3 1 2 2 1 2 3 1 3 3 2 2 2 2 2 3 2 3 3 3 3 3 </code></pre> <p>and add a digit in front of them. You can add "1" in front of all of them. You can only add "2" in front of the "2" or higher, and "3" to the one with only "3"s. Now you can see the recursive structure.</p> <p>For a simple example, to find the index of ( 2, 4, 4 ) with D = 3, K = 5:</p> <pre><code>index( 2, 4, 4 ) = # number of leading 1's, and re-index index( 3, 3 ) + count( D = 2, K = 5 ) = index( 3, 3 ) + 15 = # number of leading 1's and 2's, and re-index index( 1 ) + count( D = 1, K = 4 ) + count( D = 1, K = 3 ) + 15 = index( 1 ) + 4 + 3 + 15 = index( 1 ) + 22 = 22 </code></pre> <p>So index( 2, 4, 4 ) = 22</p> <p>Now the tricky part is figuring out count( D, K ), which is actually just C( K + D - 1, D ). You can now generalize this to your K = 15, D = 7.</p> <pre><code>// This is actually 0-based. // Really should use an array or something to make it easy to generalize, // so I'm going to skip a lot of cut and paste private static int getEntryAt( int i, int j, int k, int l, int m, int n, int o ) { int D = 7, K = 15; int total = 0; if ( i &gt; 0 ) { for ( int index = 0; index &lt; i; index++ ) { total += count( D, K - index ); } } j -= i, k -= i, l -= i, m -= i, n -= i, o -= i; D--; K -= i; // repeat for j, k, ... return count; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload