combinatorics

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2023 License: BSD-3-Clause Imports: 2 Imported by: 1

Documentation

Overview

Package combinatorics is maths primarily concerned with counting.

https://en.wikipedia.org/wiki/Combinatorics

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Factorial

func Factorial[A maths.Integer](n A) (uint64, error)

Factorial returns the product of all positive integers less than or equal to n. n must be 0 <= n <= 20.

Time complexity: O(1) - values are already precomputed for uint64 Space complexity: O(1)

https://en.wikipedia.org/wiki/Factorial

https://oeis.org/A000142

Example
n, _ := Factorial(5)
fmt.Printf("The factorial of 5 is %v.\n", n)
Output:

The factorial of 5 is 120.

func FallingFactorial

func FallingFactorial[A maths.Integer, B maths.Integer](x A, n B) (uint64, error)

FallingFactorial returns the number of ways of choosing an ordered list of length n consisting of distinct elements drawn from a collection of size x. It is possible that the result may overflow.

When x = n, this is the same as Factorial(x, n).

Time complexity: O(n) Space complexity: O(1)

https://en.wikipedia.org/wiki/Falling_and_rising_factorials

Example
n, _ := FallingFactorial(8, 3)
fmt.Printf("The number of different gold-silver-bronze podiums in an 8 person race is %v.\n", n)
Output:

The number of different gold-silver-bronze podiums in an 8 person race is 336.

func PartialPermutations

func PartialPermutations[A maths.Integer, B maths.Integer](n A, k B) (uint64, error)

PartialPermutations returns the number of ordered arrangements in which no element occurs more than once, but without the requirement of using all the elements from a given set (using only k elements)

    n!
----------
 (n - k)!

Time complexity: O(n) Space complexity: O(1)

https://en.wikipedia.org/wiki/Permutation

Example
n, _ := PartialPermutations(8, 4)
fmt.Printf("The partial permutations of 8 elements when given 4 at a time is %v.\n", n)
Output:

The partial permutations of 8 elements when given 4 at a time is 1680.

func RisingFactorial

func RisingFactorial[A maths.Integer, B maths.Integer](x A, n B) (uint64, error)

RisingFactorial returns the number of ways of arranging n items in x ordered lists where each list may have 0..n items. It is possible that the result may overflow.

Time complexity: O(n) Space complexity: O(1)

https://en.wikipedia.org/wiki/Falling_and_rising_factorials

Example
n, _ := RisingFactorial(2, 3)
fmt.Printf("3 books on 2 different shelves can be arranged in %v ways.\n", n)
Output:

3 books on 2 different shelves can be arranged in 24 ways.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL