Skip to content
FutureBlitz

    Install

    Import std.algo.combinatorics

    std.algo.combinatorics

    Every order and every choice of an array's elements, and how many of each there are.

    Permutations and Combinations: every order, or every choice, of an array's elements, made one at a time as a For ... In or an adapter asks, each a new array, in the order of the elements' positions, as Python's itertools gives them; and Factorial and Binomial, which count them. The elements are taken by position, so equal ones still make orders and choices of their own.

    std/algo/combinatorics.fb

    Types

    PermutationIterator class

    Public Class PermutationIterator<T> Implements Iterator<T[]>

    Gives every order of count elements of an array, from the first order by position.

    std/algo/combinatorics.fb, line 12

    • Public Method Next(): T[]? method #

      The next order, a new array, or Null once it has given every one.

    CombinationIterator class

    Public Class CombinationIterator<T> Implements Iterator<T[]>

    Gives every choice of count elements of an array, each in the array's order, from the first elements on.

    std/algo/combinatorics.fb, line 84

    • Public Method Next(): T[]? method #

      The next choice, a new array, or Null once it has given every one.

    Functions

    Permutations function

    Public Function Permutations<T>(items: T[]): PermutationIterator<T>

    Every order of the array's elements: n! of them, from the array's own order. The array is copied, so changing it doesn't change them.

    Examples

    Local orders: Int[][] = []
    For order In Permutations([1, 2, 3])
        orders.Add(order)
    End For
    
    Expect(orders.Count == 6)
    Expect(orders[0] == [1, 2, 3] && orders[5] == [3, 2, 1])

    std/algo/combinatorics.fb, line 155

    Permutations function

    @PanicsAtCaller Public Function Permutations<T>(items: T[], count: Int): PermutationIterator<T>

    Every order of count of the array's elements: n! / (n - count)! of them, none when count is more than n, and one empty one for 0. The array is copied. A negative count panics.

    Examples

    Local pairs: String[][] = []
    For pair In Permutations(["a", "b", "c"], 2)
        pairs.Add(pair)
    End For
    
    Expect(pairs == [["a", "b"], ["a", "c"], ["b", "a"], ["b", "c"], ["c", "a"], ["c", "b"]])

    std/algo/combinatorics.fb, line 172

    Combinations function

    @PanicsAtCaller Public Function Combinations<T>(items: T[], count: Int): CombinationIterator<T>

    Every choice of count of the array's elements, each in the array's order: Binomial(n, count) of them, none when count is more than n, and one empty one for 0. The array is copied. A negative count panics.

    Examples

    Local pairs: String[][] = []
    For pair In Combinations(["a", "b", "c"], 2)
        pairs.Add(pair)
    End For
    
    Expect(pairs == [["a", "b"], ["a", "c"], ["b", "c"]])

    std/algo/combinatorics.fb, line 192

    Factorial function

    @PanicsAtCaller Public Function Factorial(n: Int): Long

    n!, the number of orders of n things, from 0! = 1 to 20!, the most a Long holds. Any other n panics.

    Examples

    Expect(Factorial(0) == 1)
    Expect(Factorial(5) == 120)

    std/algo/combinatorics.fb, line 207

    Binomial function

    @PanicsAtCaller Public Function Binomial(n: Int, k: Int): Long

    The number of ways to choose k of n things, "n choose k": 0 for a k below 0 or above n. A negative n panics, and so does a count too large for a Long.

    Examples

    Expect(Binomial(5, 2) == 10)
    Expect(Binomial(5, 6) == 0)

    std/algo/combinatorics.fb, line 226

    Report an issue with this page or with FutureBlitz on GitHub.