Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a function I've written in R, which currently finds its (unexported) home in the <a href="http://r-forge.r-project.org/projects/lspm" rel="nofollow">LSPM</a> package. You give it the total number of items <code>n</code>, the number of items to select <code>r</code>, and the index of the combination you want <code>i</code>; it returns the values in <code>1:n</code> corresponding to combination <code>i</code>.</p> <pre><code>".combinadic" &lt;- function(n, r, i) { # http://msdn.microsoft.com/en-us/library/aa289166(VS.71).aspx # http://en.wikipedia.org/wiki/Combinadic if(i &lt; 1 | i &gt; choose(n,r)) stop("'i' must be 0 &lt; i &lt;= n!/(n-r)!") largestV &lt;- function(n, r, i) { #v &lt;- n-1 v &lt;- n # Adjusted for one-based indexing #while(choose(v,r) &gt; i) v &lt;- v-1 while(choose(v,r) &gt;= i) v &lt;- v-1 # Adjusted for one-based indexing return(v) } res &lt;- rep(NA,r) for(j in 1:r) { res[j] &lt;- largestV(n,r,i) i &lt;- i-choose(res[j],r) n &lt;- res[j] r &lt;- r-1 } res &lt;- res + 1 return(res) } </code></pre> <p>It allows you to generate each combination based on the value of the lexicographic index:</p> <pre><code>&gt; .combinadic(1344, 3, 1) [1] 3 2 1 &gt; .combinadic(1344, 3, 2) [1] 4 2 1 &gt; .combinadic(1344, 3, 403716544) [1] 1344 1343 1342 </code></pre> <p>So you just need to loop over 1:403716544 and append the results to a file. It may take awhile, but it's at least feasible (see Dirk's answer). You also may need to do it in several loops, since the vector <code>1:403716544</code> will not fit in memory on my machine.</p> <p>Or you could just port the R code to C/C++ and do the looping / writing there, since it would be <em>a lot</em> faster.</p>
 

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