Click or drag to resize

Combination Constructor (Int32, Int32, Int64)

Initializes a new combination of the supplied rank with the supplied number of picks from the supplied number of choices.

Namespace:  Kaos.Combinatorics
Assembly:  KaosCombinatorics (in KaosCombinatorics.dll) Version: 6.0.0.0
Syntax
C#
public Combination(
	int choices,
	int picks,
	long rank
)

Parameters

choices
Type: SystemInt32
Number of values to pick from.
picks
Type: SystemInt32
Number of elements in the sequence.
rank
Type: SystemInt64
Row index in the ordered Combination table.
Exceptions
ExceptionCondition
ArgumentOutOfRangeExceptionWhen negative value supplied; when picks greater than choices.
OverflowExceptionWhen too many choices.
Remarks

Supplying a value for choices that is greater than picks will instantiate a k-combination also known as a pick-combination.

If the supplied rank is out of the range (0..RowCount-1), it will be normalized to the valid range. For example, a value of -1 will produce the last row in the ordered table.

Examples
C#
using System;
using System.Linq;
using Kaos.Combinatorics;

namespace ExampleApp
{
    class CnExample05
    {
        static void Main()
        {
            // Create a k-combination:
            var cn = new Combination (choices:10, picks:7, rank:110);
            Console.WriteLine ($"n={cn.Choices}, k={cn.Picks}, rank={cn.Rank}:\n");

            // Access elements using the default enumerator:
            var text = String.Concat (cn.Select (ei => (char) ('A' + ei)));
            Console.WriteLine ($"{text}\n");

            // Access elements using the indexer:
            for (int i = 0; i < cn.Picks; ++i)
                Console.WriteLine ($"Element at {i} is {cn[i]}");
        }

        /* Output:

        n=10, k=7, rank=110:

        BDFGHIJ

        Element at 0 is 1
        Element at 1 is 3
        Element at 2 is 5
        Element at 3 is 6
        Element at 4 is 7
        Element at 5 is 8
        Element at 6 is 9

        */
    }
}
See Also