Documentation ¶
Overview ¶
Package combinatorics is maths primarily concerned with counting.
Index ¶
- func Factorial[A maths.Integer](n A) (uint64, error)
- func FallingFactorial[A maths.Integer, B maths.Integer](x A, n B) (uint64, error)
- func PartialPermutations[A maths.Integer, B maths.Integer](n A, k B) (uint64, error)
- func RisingFactorial[A maths.Integer, B maths.Integer](x A, n B) (uint64, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Factorial ¶
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
Example ¶
n, _ := Factorial(5) fmt.Printf("The factorial of 5 is %v.\n", n)
Output: The factorial of 5 is 120.
func FallingFactorial ¶
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 ¶
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 ¶
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.