Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems you are looking for a fast algorithm to calculate all k-combinations. The following Delphi code is a direct translation of the C code found here: <a href="http://compprog.wordpress.com/2007/10/17/generating-combinations-1/" rel="noreferrer">Generating Combinations</a>. I even fixed a bug in that code!</p> <pre><code>program kCombinations; {$APPTYPE CONSOLE} // Prints out a combination like {1, 2} procedure printc(const comb: array of Integer; k: Integer); var i: Integer; begin Write('{'); for i := 0 to k-1 do begin Write(comb[i]+1); if i&lt;k-1 then Write(','); end; Writeln('}'); end; (* Generates the next combination of n elements as k after comb comb =&gt; the previous combination ( use (0, 1, 2, ..., k) for first) k =&gt; the size of the subsets to generate n =&gt; the size of the original set Returns: True if a valid combination was found, False otherwise *) function next_comb(var comb: array of Integer; k, n: Integer): Boolean; var i: Integer; begin i := k - 1; inc(comb[i]); while (i&gt;0) and (comb[i]&gt;=n-k+1+i) do begin dec(i); inc(comb[i]); end; if comb[0]&gt;n-k then// Combination (n-k, n-k+1, ..., n) reached begin // No more combinations can be generated Result := False; exit; end; // comb now looks like (..., x, n, n, n, ..., n). // Turn it into (..., x, x + 1, x + 2, ...) for i := i+1 to k-1 do comb[i] := comb[i-1]+1; Result := True; end; procedure Main; const n = 4;// The size of the set; for {1, 2, 3, 4} it's 4 k = 2;// The size of the subsets; for {1, 2}, {1, 3}, ... it's 2 var i: Integer; comb: array of Integer; begin SetLength(comb, k);// comb[i] is the index of the i-th element in the combination //Setup comb for the initial combination for i := 0 to k-1 do comb[i] := i; // Print the first combination printc(comb, k); // Generate and print all the other combinations while next_comb(comb, k, n) do printc(comb, k); end; begin Main; Readln; end. </code></pre> <p><strong>Output</strong></p> <pre><code>{1,2} {1,3} {1,4} {2,3} {2,4} {3,4} </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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