Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 4183461[1X16 [33X[0;0YCombinatorics[133X[101X23[33X[0;0YThis chapter describes functions that deal with combinatorics. We mainly4concentrate on two areas. One is about [13Xselections[113X, that is the ways one can5select elements from a set. The other is about [13Xpartitions[113X, that is the ways6one can partition a set into the union of pairwise disjoint subsets.[133X789[1X16.1 [33X[0;0YCombinatorial Numbers[133X[101X1011[1X16.1-1 Factorial[101X1213[33X[1;0Y[29X[2XFactorial[102X( [3Xn[103X ) [32X function[133X1415[33X[0;0Yreturns the [13Xfactorial[113X [22Xn![122X of the positive integer [3Xn[103X, which is defined as the16product [22X1 ⋅ 2 ⋅ 3 ⋯ n[122X.[133X1718[33X[0;0Y[22Xn![122X is the number of permutations of a set of [22Xn[122X elements. [22X1 / n![122X is the19coefficient of [22Xx^n[122X in the formal series [22Xexp(x)[122X, which is the generating20function for factorial.[133X2122[4X[32X Example [32X[104X23[4X[25Xgap>[125X [27XList( [0..10], Factorial );[127X[104X24[4X[28X[ 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 ][128X[104X25[4X[25Xgap>[125X [27XFactorial( 30 );[127X[104X26[4X[28X265252859812191058636308480000000[128X[104X27[4X[32X[104X2829[33X[0;0Y[2XPermutationsList[102X ([14X16.2-12[114X) computes the set of all permutations of a list.[133X3031[1X16.1-2 Binomial[101X3233[33X[1;0Y[29X[2XBinomial[102X( [3Xn[103X, [3Xk[103X ) [32X function[133X3435[33X[0;0Yreturns the [13Xbinomial coefficient[113X [22X{n choose k}[122X of integers [3Xn[103X and [3Xk[103X, which is36defined as [22Xn! / (k! (n-k)!)[122X (see [2XFactorial[102X ([14X16.1-1[114X)). We define [22X{0 choose 0}37= 1, {n choose k} = 0[122X if [22Xk < 0[122X or [22Xn < k[122X, and [22X{n choose k} = (-1)^k {-n+k-138choose k}[122X if [22Xn < 0[122X, which is consistent with the equivalent definition [22X{n39choose k} = {n-1 choose k} + {n-1 choose k-1}[122X.[133X4041[33X[0;0Y[22X{n choose k}[122X is the number of combinations with [22Xk[122X elements, i.e., the number42of subsets with [22Xk[122X elements, of a set with [22Xn[122X elements. [22X{n choose k}[122X is the43coefficient of the term [22Xx^k[122X of the polynomial [22X(x + 1)^n[122X, which is the44generating function for [22X{n choose .}[122X, hence the name.[133X4546[4X[32X Example [32X[104X47[4X[25Xgap>[125X [27X# Knuth calls this the trademark of Binomial:[127X[104X48[4X[25Xgap>[125X [27XList( [0..4], k->Binomial( 4, k ) );[127X[104X49[4X[28X[ 1, 4, 6, 4, 1 ][128X[104X50[4X[25Xgap>[125X [27XList( [0..6], n->List( [0..6], k->Binomial( n, k ) ) );;[127X[104X51[4X[25Xgap>[125X [27X# the lower triangle is called Pascal's triangle:[127X[104X52[4X[25Xgap>[125X [27XPrintArray( last );[127X[104X53[4X[28X[ [ 1, 0, 0, 0, 0, 0, 0 ],[128X[104X54[4X[28X [ 1, 1, 0, 0, 0, 0, 0 ],[128X[104X55[4X[28X [ 1, 2, 1, 0, 0, 0, 0 ],[128X[104X56[4X[28X [ 1, 3, 3, 1, 0, 0, 0 ],[128X[104X57[4X[28X [ 1, 4, 6, 4, 1, 0, 0 ],[128X[104X58[4X[28X [ 1, 5, 10, 10, 5, 1, 0 ],[128X[104X59[4X[28X [ 1, 6, 15, 20, 15, 6, 1 ] ][128X[104X60[4X[25Xgap>[125X [27XBinomial( 50, 10 );[127X[104X61[4X[28X10272278170[128X[104X62[4X[32X[104X6364[33X[0;0Y[2XNrCombinations[102X ([14X16.2-3[114X) is the generalization of [2XBinomial[102X for multisets.65[2XCombinations[102X ([14X16.2-1[114X) computes the set of all combinations of a multiset.[133X6667[1X16.1-3 Bell[101X6869[33X[1;0Y[29X[2XBell[102X( [3Xn[103X ) [32X function[133X7071[33X[0;0Yreturns the [13XBell number[113X [22XB(n)[122X. The Bell numbers are defined by [22XB(0) = 1[122X and72the recurrence [22XB(n+1) = ∑_{k = 0}^n {n choose k} B(k)[122X.[133X7374[33X[0;0Y[22XB(n)[122X is the number of ways to partition a set of [3Xn[103X elements into pairwise75disjoint nonempty subsets (see [2XPartitionsSet[102X ([14X16.2-16[114X)). This implies of76course that [22XB(n) = ∑_{k = 0}^n S_2(n,k)[122X (see [2XStirling2[102X ([14X16.1-6[114X)). [22XB(n)/n![122X is77the coefficient of [22Xx^n[122X in the formal series [22Xexp( exp(x)-1 )[122X, which is the78generating function for [22XB(n)[122X.[133X7980[4X[32X Example [32X[104X81[4X[25Xgap>[125X [27XList( [0..6], n -> Bell( n ) );[127X[104X82[4X[28X[ 1, 1, 2, 5, 15, 52, 203 ][128X[104X83[4X[25Xgap>[125X [27XBell( 14 );[127X[104X84[4X[28X190899322[128X[104X85[4X[32X[104X8687[1X16.1-4 Bernoulli[101X8889[33X[1;0Y[29X[2XBernoulli[102X( [3Xn[103X ) [32X function[133X9091[33X[0;0Yreturns the [3Xn[103X-th [13XBernoulli number[113X [22XB_n[122X, which is defined by [22XB_0 = 1[122X and [22XB_n =92-∑_{k = 0}^{n-1} {n+1 choose k} B_k/(n+1)[122X.[133X9394[33X[0;0Y[22XB_n / n![122X is the coefficient of [22Xx^n[122X in the power series of [22Xx / (exp(x)-1)[122X.95Except for [22XB_1 = -1/2[122X the Bernoulli numbers for odd indices are zero.[133X9697[4X[32X Example [32X[104X98[4X[25Xgap>[125X [27XBernoulli( 4 );[127X[104X99[4X[28X-1/30[128X[104X100[4X[25Xgap>[125X [27XBernoulli( 10 );[127X[104X101[4X[28X5/66[128X[104X102[4X[25Xgap>[125X [27XBernoulli( 12 ); # there is no simple pattern in Bernoulli numbers[127X[104X103[4X[28X-691/2730[128X[104X104[4X[25Xgap>[125X [27XBernoulli( 50 ); # and they grow fairly fast[127X[104X105[4X[28X495057205241079648212477525/66[128X[104X106[4X[32X[104X107108[1X16.1-5 Stirling1[101X109110[33X[1;0Y[29X[2XStirling1[102X( [3Xn[103X, [3Xk[103X ) [32X function[133X111112[33X[0;0Yreturns the [13XStirling number of the first kind[113X [22XS_1(n,k)[122X of the integers [3Xn[103X and113[3Xk[103X. Stirling numbers of the first kind are defined by [22XS_1(0,0) = 1[122X, [22XS_1(n,0)114= S_1(0,k) = 0[122X if [22Xn, k ne 0[122X and the recurrence [22XS_1(n,k) = (n-1) S_1(n-1,k) +115S_1(n-1,k-1)[122X.[133X116117[33X[0;0Y[22XS_1(n,k)[122X is the number of permutations of [3Xn[103X points with [3Xk[103X cycles. Stirling118numbers of the first kind appear as coefficients in the series [22Xn! {x choose119n} = ∑_{k = 0}^n S_1(n,k) x^k[122X which is the generating function for Stirling120numbers of the first kind. Note the similarity to [22Xx^n = ∑_{k = 0}^n S_2(n,k)121k! {x choose k}[122X (see [2XStirling2[102X ([14X16.1-6[114X)). Also the definition of [22XS_1[122X implies122[22XS_1(n,k) = S_2(-k,-n)[122X if [22Xn, k < 0[122X. There are many formulae relating Stirling123numbers of the first kind to Stirling numbers of the second kind, Bell124numbers, and Binomial coefficients.[133X125126[4X[32X Example [32X[104X127[4X[25Xgap>[125X [27X# Knuth calls this the trademark of S_1:[127X[104X128[4X[25Xgap>[125X [27XList( [0..4], k -> Stirling1( 4, k ) );[127X[104X129[4X[28X[ 0, 6, 11, 6, 1 ][128X[104X130[4X[25Xgap>[125X [27XList( [0..6], n->List( [0..6], k->Stirling1( n, k ) ) );;[127X[104X131[4X[25Xgap>[125X [27X# note the similarity with Pascal's triangle for Binomial numbers[127X[104X132[4X[25Xgap>[125X [27XPrintArray( last );[127X[104X133[4X[28X[ [ 1, 0, 0, 0, 0, 0, 0 ],[128X[104X134[4X[28X [ 0, 1, 0, 0, 0, 0, 0 ],[128X[104X135[4X[28X [ 0, 1, 1, 0, 0, 0, 0 ],[128X[104X136[4X[28X [ 0, 2, 3, 1, 0, 0, 0 ],[128X[104X137[4X[28X [ 0, 6, 11, 6, 1, 0, 0 ],[128X[104X138[4X[28X [ 0, 24, 50, 35, 10, 1, 0 ],[128X[104X139[4X[28X [ 0, 120, 274, 225, 85, 15, 1 ] ][128X[104X140[4X[25Xgap>[125X [27XStirling1(50,10);[127X[104X141[4X[28X101623020926367490059043797119309944043405505380503665627365376[128X[104X142[4X[32X[104X143144[1X16.1-6 Stirling2[101X145146[33X[1;0Y[29X[2XStirling2[102X( [3Xn[103X, [3Xk[103X ) [32X function[133X147148[33X[0;0Yreturns the [13XStirling number of the second kind[113X [22XS_2(n,k)[122X of the integers [3Xn[103X149and [3Xk[103X. Stirling numbers of the second kind are defined by [22XS_2(0,0) = 1[122X,150[22XS_2(n,0) = S_2(0,k) = 0[122X if [22Xn, k ne 0[122X and the recurrence [22XS_2(n,k) = k151S_2(n-1,k) + S_2(n-1,k-1)[122X.[133X152153[33X[0;0Y[22XS_2(n,k)[122X is the number of ways to partition a set of [3Xn[103X elements into [3Xk[103X154pairwise disjoint nonempty subsets (see [2XPartitionsSet[102X ([14X16.2-16[114X)). Stirling155numbers of the second kind appear as coefficients in the expansion of [22Xx^n =156∑_{k = 0}^n S_2(n,k) k! {x choose k}[122X. Note the similarity to [22Xn! {x choose n}157= ∑_{k = 0}^n S_1(n,k) x^k[122X (see [2XStirling1[102X ([14X16.1-5[114X)). Also the definition of158[22XS_2[122X implies [22XS_2(n,k) = S_1(-k,-n)[122X if [22Xn, k < 0[122X. There are many formulae159relating Stirling numbers of the second kind to Stirling numbers of the160first kind, Bell numbers, and Binomial coefficients.[133X161162[4X[32X Example [32X[104X163[4X[25Xgap>[125X [27X# Knuth calls this the trademark of S_2:[127X[104X164[4X[25Xgap>[125X [27XList( [0..4], k->Stirling2( 4, k ) );[127X[104X165[4X[28X[ 0, 1, 7, 6, 1 ][128X[104X166[4X[25Xgap>[125X [27XList( [0..6], n->List( [0..6], k->Stirling2( n, k ) ) );;[127X[104X167[4X[25Xgap>[125X [27X# note the similarity with Pascal's triangle for Binomial numbers[127X[104X168[4X[25Xgap>[125X [27XPrintArray( last );[127X[104X169[4X[28X[ [ 1, 0, 0, 0, 0, 0, 0 ],[128X[104X170[4X[28X [ 0, 1, 0, 0, 0, 0, 0 ],[128X[104X171[4X[28X [ 0, 1, 1, 0, 0, 0, 0 ],[128X[104X172[4X[28X [ 0, 1, 3, 1, 0, 0, 0 ],[128X[104X173[4X[28X [ 0, 1, 7, 6, 1, 0, 0 ],[128X[104X174[4X[28X [ 0, 1, 15, 25, 10, 1, 0 ],[128X[104X175[4X[28X [ 0, 1, 31, 90, 65, 15, 1 ] ][128X[104X176[4X[25Xgap>[125X [27XStirling2( 50, 10 );[127X[104X177[4X[28X26154716515862881292012777396577993781727011[128X[104X178[4X[32X[104X179180181[1X16.2 [33X[0;0YCombinations, Arrangements and Tuples[133X[101X182183[1X16.2-1 Combinations[101X184185[33X[1;0Y[29X[2XCombinations[102X( [3Xmset[103X[, [3Xk[103X] ) [32X function[133X186187[33X[0;0Yreturns the set of all combinations of the multiset [3Xmset[103X (a list of objects188which may contain the same object several times) with [3Xk[103X elements; if [3Xk[103X is189not given it returns all combinations of [3Xmset[103X.[133X190191[33X[0;0YA [13Xcombination[113X of [3Xmset[103X is an unordered selection without repetitions and is192represented by a sorted sublist of [3Xmset[103X. If [3Xmset[103X is a proper set, there are193[22X{|[3Xmset[103X| choose [3Xk[103X}[122X (see [2XBinomial[102X ([14X16.1-2[114X)) combinations with [3Xk[103X elements, and194the set of all combinations is just the [13Xpower set[113X of [3Xmset[103X, which contains195all [13Xsubsets[113X of [3Xmset[103X and has cardinality [22X2^{|[3Xmset[103X|}[122X.[133X196197[33X[0;0YTo loop over combinations of a larger multiset use [2XIteratorOfCombinations[102X198([14X16.2-2[114X) which produces combinations one by one and may save a lot of199memory. Another memory efficient representation of the list of all200combinations is provided by [2XEnumeratorOfCombinations[102X ([14X16.2-2[114X).[133X201202203[1X16.2-2 [33X[0;0YIterator and enumerator of combinations[133X[101X204205[33X[1;0Y[29X[2XIteratorOfCombinations[102X( [3Xmset[103X[, [3Xk[103X] ) [32X function[133X206[33X[1;0Y[29X[2XEnumeratorOfCombinations[102X( [3Xmset[103X ) [32X function[133X207208[33X[0;0Y[2XIteratorOfCombinations[102X returns an [2XIterator[102X ([14X30.8-1[114X) for combinations (see209[2XCombinations[102X ([14X16.2-1[114X)) of the given multiset [3Xmset[103X. If a non-negative integer210[3Xk[103X is given as second argument then only the combinations with [3Xk[103X entries are211produced, otherwise all combinations.[133X212213[33X[0;0Y[2XEnumeratorOfCombinations[102X returns an [2XEnumerator[102X ([14X30.3-2[114X) of the given214multiset [3Xmset[103X. Currently only a variant without second argument [3Xk[103X is215implemented.[133X216217[33X[0;0YThe ordering of combinations from these functions can be different and also218different from the list returned by [2XCombinations[102X ([14X16.2-1[114X).[133X219220[4X[32X Example [32X[104X221[4X[25Xgap>[125X [27Xm:=[1..15];; Add(m, 15);[127X[104X222[4X[25Xgap>[125X [27XNrCombinations(m);[127X[104X223[4X[28X49152[128X[104X224[4X[25Xgap>[125X [27Xi := 0;; for c in Combinations(m) do i := i+1; od;[127X[104X225[4X[25Xgap>[125X [27Xi;[127X[104X226[4X[28X49152[128X[104X227[4X[25Xgap>[125X [27Xcm := EnumeratorOfCombinations(m);;[127X[104X228[4X[25Xgap>[125X [27Xcm[1000];[127X[104X229[4X[28X[ 1, 2, 3, 6, 7, 8, 9, 10 ][128X[104X230[4X[25Xgap>[125X [27XPosition(cm, [1,13,15,15]);[127X[104X231[4X[28X36866[128X[104X232[4X[32X[104X233234[1X16.2-3 NrCombinations[101X235236[33X[1;0Y[29X[2XNrCombinations[102X( [3Xmset[103X[, [3Xk[103X] ) [32X function[133X237238[33X[0;0Yreturns the number of [10XCombinations([3Xmset[103X[10X,[3Xk[103X[10X)[110X.[133X239240[4X[32X Example [32X[104X241[4X[25Xgap>[125X [27XCombinations( [1,2,2,3] );[127X[104X242[4X[28X[ [ ], [ 1 ], [ 1, 2 ], [ 1, 2, 2 ], [ 1, 2, 2, 3 ], [ 1, 2, 3 ], [128X[104X243[4X[28X [ 1, 3 ], [ 2 ], [ 2, 2 ], [ 2, 2, 3 ], [ 2, 3 ], [ 3 ] ][128X[104X244[4X[25Xgap>[125X [27X# number of different hands in a game of poker:[127X[104X245[4X[25Xgap>[125X [27XNrCombinations( [1..52], 5 );[127X[104X246[4X[28X2598960[128X[104X247[4X[32X[104X248249[33X[0;0YThe function [2XArrangements[102X ([14X16.2-4[114X) computes ordered selections without250repetitions, [2XUnorderedTuples[102X ([14X16.2-6[114X) computes unordered selections with251repetitions, and [2XTuples[102X ([14X16.2-8[114X) computes ordered selections with252repetitions.[133X253254[1X16.2-4 Arrangements[101X255256[33X[1;0Y[29X[2XArrangements[102X( [3Xmset[103X[, [3Xk[103X] ) [32X function[133X257258[33X[0;0Yreturns the set of arrangements of the multiset [3Xmset[103X that contain [3Xk[103X259elements. If [3Xk[103X is not given it returns all arrangements of [3Xmset[103X.[133X260261[33X[0;0YAn [13Xarrangement[113X of [3Xmset[103X is an ordered selection without repetitions and is262represented by a list that contains only elements from [3Xmset[103X, but maybe in a263different order. If [3Xmset[103X is a proper set there are [22X|mset|! / (|mset|-k)![122X264(see [2XFactorial[102X ([14X16.1-1[114X)) arrangements with [3Xk[103X elements.[133X265266[1X16.2-5 NrArrangements[101X267268[33X[1;0Y[29X[2XNrArrangements[102X( [3Xmset[103X[, [3Xk[103X] ) [32X function[133X269270[33X[0;0Yreturns the number of [10XArrangements([3Xmset[103X[10X,[3Xk[103X[10X)[110X.[133X271272[33X[0;0YAs an example of arrangements of a multiset, think of the game Scrabble.273Suppose you have the six characters of the word [10X"settle"[110X and you have to274make a four letter word. Then the possibilities are given by[133X275276[4X[32X Example [32X[104X277[4X[25Xgap>[125X [27XArrangements( ["s","e","t","t","l","e"], 4 );[127X[104X278[4X[28X[ [ "e", "e", "l", "s" ], [ "e", "e", "l", "t" ], [ "e", "e", "s", "l" ],[128X[104X279[4X[28X [ "e", "e", "s", "t" ], [ "e", "e", "t", "l" ], [ "e", "e", "t", "s" ],[128X[104X280[4X[28X ... 93 more possibilities ...[128X[104X281[4X[28X [ "t", "t", "l", "s" ], [ "t", "t", "s", "e" ], [ "t", "t", "s", "l" ] ][128X[104X282[4X[32X[104X283284[33X[0;0YCan you find the five proper English words, where [10X"lets"[110X does not count?285Note that the fact that the list returned by [2XArrangements[102X ([14X16.2-4[114X) is a286proper set means in this example that the possibilities are listed in the287same order as they appear in the dictionary.[133X288289[4X[32X Example [32X[104X290[4X[25Xgap>[125X [27XNrArrangements( ["s","e","t","t","l","e"] );[127X[104X291[4X[28X523[128X[104X292[4X[32X[104X293294[33X[0;0YThe function [2XCombinations[102X ([14X16.2-1[114X) computes unordered selections without295repetitions, [2XUnorderedTuples[102X ([14X16.2-6[114X) computes unordered selections with296repetitions, and [2XTuples[102X ([14X16.2-8[114X) computes ordered selections with297repetitions.[133X298299[1X16.2-6 UnorderedTuples[101X300301[33X[1;0Y[29X[2XUnorderedTuples[102X( [3Xset[103X, [3Xk[103X ) [32X function[133X302303[33X[0;0Yreturns the set of all unordered tuples of length [3Xk[103X of the set [3Xset[103X.[133X304305[33X[0;0YAn [13Xunordered tuple[113X of length [3Xk[103X of [3Xset[103X is an unordered selection with306repetitions of [3Xset[103X and is represented by a sorted list of length [3Xk[103X307containing elements from [3Xset[103X. There are [22X{|set| + k - 1 choose k}[122X (see308[2XBinomial[102X ([14X16.1-2[114X)) such unordered tuples.[133X309310[33X[0;0YNote that the fact that [2XUnorderedTuples[102X returns a set implies that the last311index runs fastest. That means the first tuple contains the smallest element312from [3Xset[103X [3Xk[103X times, the second tuple contains the smallest element of [3Xset[103X at313all positions except at the last positions, where it contains the second314smallest element from [3Xset[103X and so on.[133X315316[1X16.2-7 NrUnorderedTuples[101X317318[33X[1;0Y[29X[2XNrUnorderedTuples[102X( [3Xset[103X, [3Xk[103X ) [32X function[133X319320[33X[0;0Yreturns the number of [10XUnorderedTuples([3Xset[103X[10X,[3Xk[103X[10X)[110X.[133X321322[33X[0;0YAs an example for unordered tuples think of a poker-like game played with 5323dice. Then each possible hand corresponds to an unordered five-tuple from324the set [22X{ 1, 2, ..., 6 }[122X.[133X325326[4X[32X Example [32X[104X327[4X[25Xgap>[125X [27XNrUnorderedTuples( [1..6], 5 );[127X[104X328[4X[28X252[128X[104X329[4X[25Xgap>[125X [27XUnorderedTuples( [1..6], 5 );[127X[104X330[4X[28X[ [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 2 ], [ 1, 1, 1, 1, 3 ], [ 1, 1, 1, 1, 4 ],[128X[104X331[4X[28X [ 1, 1, 1, 1, 5 ], [ 1, 1, 1, 1, 6 ], [ 1, 1, 1, 2, 2 ], [ 1, 1, 1, 2, 3 ],[128X[104X332[4X[28X ... 100 more tuples ...[128X[104X333[4X[28X [ 1, 3, 5, 5, 6 ], [ 1, 3, 5, 6, 6 ], [ 1, 3, 6, 6, 6 ], [ 1, 4, 4, 4, 4 ],[128X[104X334[4X[28X ... 100 more tuples ...[128X[104X335[4X[28X [ 3, 3, 5, 5, 5 ], [ 3, 3, 5, 5, 6 ], [ 3, 3, 5, 6, 6 ], [ 3, 3, 6, 6, 6 ],[128X[104X336[4X[28X ... 32 more tuples ...[128X[104X337[4X[28X [ 5, 5, 5, 6, 6 ], [ 5, 5, 6, 6, 6 ], [ 5, 6, 6, 6, 6 ], [ 6, 6, 6, 6, 6 ] ][128X[104X338[4X[32X[104X339340[33X[0;0YThe function [2XCombinations[102X ([14X16.2-1[114X) computes unordered selections without341repetitions, [2XArrangements[102X ([14X16.2-4[114X) computes ordered selections without342repetitions, and [2XTuples[102X ([14X16.2-8[114X) computes ordered selections with343repetitions.[133X344345[1X16.2-8 Tuples[101X346347[33X[1;0Y[29X[2XTuples[102X( [3Xset[103X, [3Xk[103X ) [32X function[133X348349[33X[0;0Yreturns the set of all ordered tuples of length [3Xk[103X of the set [3Xset[103X.[133X350351[33X[0;0YAn [13Xordered tuple[113X of length [3Xk[103X of [3Xset[103X is an ordered selection with repetition352and is represented by a list of length [3Xk[103X containing elements of [3Xset[103X. There353are [22X|[3Xset[103X|^[3Xk[103X[122X such ordered tuples.[133X354355[33X[0;0YNote that the fact that [2XTuples[102X returns a set implies that the last index356runs fastest. That means the first tuple contains the smallest element from357[3Xset[103X [3Xk[103X times, the second tuple contains the smallest element of [3Xset[103X at all358positions except at the last positions, where it contains the second359smallest element from [3Xset[103X and so on.[133X360361[1X16.2-9 EnumeratorOfTuples[101X362363[33X[1;0Y[29X[2XEnumeratorOfTuples[102X( [3Xset[103X, [3Xk[103X ) [32X function[133X364365[33X[0;0YThis function is referred to as an example of enumerators that are defined366by functions but are not constructed from a domain. The result is equal to367that of [10XTuples( [3Xset[103X[10X, [3Xk[103X[10X )[110X. However, the entries are not stored physically in368the list but are created/identified on demand.[133X369370[1X16.2-10 IteratorOfTuples[101X371372[33X[1;0Y[29X[2XIteratorOfTuples[102X( [3Xset[103X, [3Xk[103X ) [32X function[133X373374[33X[0;0YFor a set [3Xset[103X and a positive integer [3Xk[103X, [2XIteratorOfTuples[102X returns an iterator375(see [14X30.8[114X) of the set of all ordered tuples (see [2XTuples[102X ([14X16.2-8[114X)) of length376[3Xk[103X of the set [3Xset[103X. The tuples are returned in lexicographic order.[133X377378[1X16.2-11 NrTuples[101X379380[33X[1;0Y[29X[2XNrTuples[102X( [3Xset[103X, [3Xk[103X ) [32X function[133X381382[33X[0;0Yreturns the number of [10XTuples([3Xset[103X[10X,[3Xk[103X[10X)[110X.[133X383384[4X[32X Example [32X[104X385[4X[25Xgap>[125X [27XTuples( [1,2,3], 2 );[127X[104X386[4X[28X[ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ], [128X[104X387[4X[28X [ 3, 1 ], [ 3, 2 ], [ 3, 3 ] ][128X[104X388[4X[25Xgap>[125X [27XNrTuples( [1..10], 5 );[127X[104X389[4X[28X100000[128X[104X390[4X[32X[104X391392[33X[0;0Y[10XTuples([3Xset[103X[10X,[3Xk[103X[10X)[110X can also be viewed as the [3Xk[103X-fold cartesian product of [3Xset[103X (see393[2XCartesian[102X ([14X21.20-16[114X)).[133X394395[33X[0;0YThe function [2XCombinations[102X ([14X16.2-1[114X) computes unordered selections without396repetitions, [2XArrangements[102X ([14X16.2-4[114X) computes ordered selections without397repetitions, and finally the function [2XUnorderedTuples[102X ([14X16.2-6[114X) computes398unordered selections with repetitions.[133X399400[1X16.2-12 PermutationsList[101X401402[33X[1;0Y[29X[2XPermutationsList[102X( [3Xmset[103X ) [32X function[133X403404[33X[0;0Y[2XPermutationsList[102X returns the set of permutations of the multiset [3Xmset[103X.[133X405406[33X[0;0YA [13Xpermutation[113X is represented by a list that contains exactly the same407elements as [3Xmset[103X, but possibly in different order. If [3Xmset[103X is a proper set408there are [22X|[3Xmset[103X| ![122X (see [2XFactorial[102X ([14X16.1-1[114X)) such permutations. Otherwise if409the first elements appears [22Xk_1[122X times, the second element appears [22Xk_2[122X times410and so on, the number of permutations is [22X|[3Xmset[103X| ! / (k_1! k_2! ...)[122X, which411is sometimes called multinomial coefficient.[133X412413[1X16.2-13 NrPermutationsList[101X414415[33X[1;0Y[29X[2XNrPermutationsList[102X( [3Xmset[103X ) [32X function[133X416417[33X[0;0Yreturns the number of [10XPermutationsList([3Xmset[103X[10X)[110X.[133X418419[4X[32X Example [32X[104X420[4X[25Xgap>[125X [27XPermutationsList( [1,2,3] );[127X[104X421[4X[28X[ [ 1, 2, 3 ], [ 1, 3, 2 ], [ 2, 1, 3 ], [ 2, 3, 1 ], [ 3, 1, 2 ], [128X[104X422[4X[28X [ 3, 2, 1 ] ][128X[104X423[4X[25Xgap>[125X [27XPermutationsList( [1,1,2,2] );[127X[104X424[4X[28X[ [ 1, 1, 2, 2 ], [ 1, 2, 1, 2 ], [ 1, 2, 2, 1 ], [ 2, 1, 1, 2 ], [128X[104X425[4X[28X [ 2, 1, 2, 1 ], [ 2, 2, 1, 1 ] ][128X[104X426[4X[25Xgap>[125X [27XNrPermutationsList( [1,2,2,3,3,3,4,4,4,4] );[127X[104X427[4X[28X12600[128X[104X428[4X[32X[104X429430[33X[0;0YThe function [2XArrangements[102X ([14X16.2-4[114X) is the generalization of [2XPermutationsList[102X431([14X16.2-12[114X) that allows you to specify the size of the permutations.432[2XDerangements[102X ([14X16.2-14[114X) computes permutations that have no fixed points.[133X433434[1X16.2-14 Derangements[101X435436[33X[1;0Y[29X[2XDerangements[102X( [3Xlist[103X ) [32X function[133X437438[33X[0;0Yreturns the set of all derangements of the list [3Xlist[103X.[133X439440[33X[0;0YA [13Xderangement[113X is a fixpointfree permutation of [3Xlist[103X and is represented by a441list that contains exactly the same elements as [3Xlist[103X, but in such an order442that the derangement has at no position the same element as [3Xlist[103X. If the443list [3Xlist[103X contains no element twice there are exactly [22X|[3Xlist[103X|! (1/2! - 1/3! +4441/4! - ⋯ + (-1)^n / n!)[122X derangements.[133X445446[33X[0;0YNote that the ratio [10XNrPermutationsList( [ 1 .. n ] ) / NrDerangements( [ 1447.. n ] )[110X, which is [22Xn! / (n! (1/2! - 1/3! + 1/4! - ⋯ + (-1)^n / n!))[122X is an448approximation for the base of the natural logarithm [22Xe = 2.7182818285...[122X,449which is correct to about [22Xn[122X digits.[133X450451[1X16.2-15 NrDerangements[101X452453[33X[1;0Y[29X[2XNrDerangements[102X( [3Xlist[103X ) [32X function[133X454455[33X[0;0Yreturns the number of [10XDerangements([3Xlist[103X[10X)[110X.[133X456457[33X[0;0YAs an example of derangements suppose that you have to send four different458letters to four different people. Then a derangement corresponds to a way to459send those letters such that no letter reaches the intended person.[133X460461[4X[32X Example [32X[104X462[4X[25Xgap>[125X [27XDerangements( [1,2,3,4] );[127X[104X463[4X[28X[ [ 2, 1, 4, 3 ], [ 2, 3, 4, 1 ], [ 2, 4, 1, 3 ], [ 3, 1, 4, 2 ], [128X[104X464[4X[28X [ 3, 4, 1, 2 ], [ 3, 4, 2, 1 ], [ 4, 1, 2, 3 ], [ 4, 3, 1, 2 ], [128X[104X465[4X[28X [ 4, 3, 2, 1 ] ][128X[104X466[4X[25Xgap>[125X [27XNrDerangements( [1..10] );[127X[104X467[4X[28X1334961[128X[104X468[4X[25Xgap>[125X [27XInt( 10^7*NrPermutationsList([1..10])/last );[127X[104X469[4X[28X27182816[128X[104X470[4X[25Xgap>[125X [27XDerangements( [1,1,2,2,3,3] );[127X[104X471[4X[28X[ [ 2, 2, 3, 3, 1, 1 ], [ 2, 3, 1, 3, 1, 2 ], [ 2, 3, 1, 3, 2, 1 ], [128X[104X472[4X[28X [ 2, 3, 3, 1, 1, 2 ], [ 2, 3, 3, 1, 2, 1 ], [ 3, 2, 1, 3, 1, 2 ], [128X[104X473[4X[28X [ 3, 2, 1, 3, 2, 1 ], [ 3, 2, 3, 1, 1, 2 ], [ 3, 2, 3, 1, 2, 1 ], [128X[104X474[4X[28X [ 3, 3, 1, 1, 2, 2 ] ][128X[104X475[4X[25Xgap>[125X [27XNrDerangements( [1,2,2,3,3,3,4,4,4,4] );[127X[104X476[4X[28X338[128X[104X477[4X[32X[104X478479[33X[0;0YThe function [2XPermutationsList[102X ([14X16.2-12[114X) computes all permutations of a list.[133X480481[1X16.2-16 PartitionsSet[101X482483[33X[1;0Y[29X[2XPartitionsSet[102X( [3Xset[103X[, [3Xk[103X] ) [32X function[133X484485[33X[0;0Yreturns the set of all unordered partitions of the set [3Xset[103X into [3Xk[103X pairwise486disjoint nonempty sets. If [3Xk[103X is not given it returns all unordered487partitions of [3Xset[103X for all [3Xk[103X.[133X488489[33X[0;0YAn [13Xunordered partition[113X of [3Xset[103X is a set of pairwise disjoint nonempty sets490with union [3Xset[103X and is represented by a sorted list of such sets. There are491[22XB( |set| )[122X (see [2XBell[102X ([14X16.1-3[114X)) partitions of the set [3Xset[103X and [22XS_2( |set|, k )[122X492(see [2XStirling2[102X ([14X16.1-6[114X)) partitions with [3Xk[103X elements.[133X493494[1X16.2-17 NrPartitionsSet[101X495496[33X[1;0Y[29X[2XNrPartitionsSet[102X( [3Xset[103X[, [3Xk[103X] ) [32X function[133X497498[33X[0;0Yreturns the number of [10XPartitionsSet([3Xset[103X[10X,[3Xk[103X[10X)[110X.[133X499500[4X[32X Example [32X[104X501[4X[25Xgap>[125X [27XPartitionsSet( [1,2,3] );[127X[104X502[4X[28X[ [ [ 1 ], [ 2 ], [ 3 ] ], [ [ 1 ], [ 2, 3 ] ], [ [ 1, 2 ], [ 3 ] ], [128X[104X503[4X[28X [ [ 1, 2, 3 ] ], [ [ 1, 3 ], [ 2 ] ] ][128X[104X504[4X[25Xgap>[125X [27XPartitionsSet( [1,2,3,4], 2 );[127X[104X505[4X[28X[ [ [ 1 ], [ 2, 3, 4 ] ], [ [ 1, 2 ], [ 3, 4 ] ], [128X[104X506[4X[28X [ [ 1, 2, 3 ], [ 4 ] ], [ [ 1, 2, 4 ], [ 3 ] ], [128X[104X507[4X[28X [ [ 1, 3 ], [ 2, 4 ] ], [ [ 1, 3, 4 ], [ 2 ] ], [128X[104X508[4X[28X [ [ 1, 4 ], [ 2, 3 ] ] ][128X[104X509[4X[25Xgap>[125X [27XNrPartitionsSet( [1..6] );[127X[104X510[4X[28X203[128X[104X511[4X[25Xgap>[125X [27XNrPartitionsSet( [1..10], 3 );[127X[104X512[4X[28X9330[128X[104X513[4X[32X[104X514515[33X[0;0YNote that [2XPartitionsSet[102X ([14X16.2-16[114X) does currently not support multisets and516that there is currently no ordered counterpart.[133X517518[1X16.2-18 Partitions[101X519520[33X[1;0Y[29X[2XPartitions[102X( [3Xn[103X[, [3Xk[103X] ) [32X function[133X521522[33X[0;0Yreturns the set of all (unordered) partitions of the positive integer [3Xn[103X into523sums with [3Xk[103X summands. If [3Xk[103X is not given it returns all unordered partitions524of [3Xset[103X for all [3Xk[103X.[133X525526[33X[0;0YAn [13Xunordered partition[113X is an unordered sum [22Xn = p_1 + p_2 + ⋯ + p_k[122X of527positive integers and is represented by the list [22Xp = [ p_1, p_2, ..., p_k ][122X,528in nonincreasing order, i.e., [22Xp_1 ≥ p_2 ≥ ... ≥ p_k[122X. We write [22Xp ⊢ n[122X. There529are approximately [22Xexp(π sqrt{2/3 n}) / (4 sqrt{3} n)[122X such partitions, use530[2XNrPartitions[102X ([14X16.2-20[114X) to compute the precise number.[133X531532[33X[0;0YIf you want to loop over all partitions of some larger [3Xn[103X use the more memory533efficient [2XIteratorOfPartitions[102X ([14X16.2-19[114X).[133X534535[33X[0;0YIt is possible to associate with every partition of the integer [3Xn[103X a536conjugacy class of permutations in the symmetric group on [3Xn[103X points and vice537versa. Therefore [22Xp(n) :=[122X[10XNrPartitions[110X[22X(n)[122X is the number of conjugacy classes538of the symmetric group on [3Xn[103X points.[133X539540[33X[0;0YRamanujan found the identities [22Xp(5i+4) = 0[122X mod 5, [22Xp(7i+5) = 0[122X mod 7 and541[22Xp(11i+6) = 0[122X mod 11 and many other fascinating things about the number of542partitions.[133X543544[1X16.2-19 IteratorOfPartitions[101X545546[33X[1;0Y[29X[2XIteratorOfPartitions[102X( [3Xn[103X ) [32X function[133X547548[33X[0;0YFor a positive integer [3Xn[103X, [2XIteratorOfPartitions[102X returns an iterator549(see [14X30.8[114X) of the set of partitions of [3Xn[103X (see [2XPartitions[102X ([14X16.2-18[114X)). The550partitions of [3Xn[103X are returned in lexicographic order.[133X551552[1X16.2-20 NrPartitions[101X553554[33X[1;0Y[29X[2XNrPartitions[102X( [3Xn[103X[, [3Xk[103X] ) [32X function[133X555556[33X[0;0Yreturns the number of [10XPartitions([3Xset[103X[10X,[3Xk[103X[10X)[110X.[133X557558[4X[32X Example [32X[104X559[4X[25Xgap>[125X [27XPartitions( 7 );[127X[104X560[4X[28X[ [ 1, 1, 1, 1, 1, 1, 1 ], [ 2, 1, 1, 1, 1, 1 ], [ 2, 2, 1, 1, 1 ], [128X[104X561[4X[28X [ 2, 2, 2, 1 ], [ 3, 1, 1, 1, 1 ], [ 3, 2, 1, 1 ], [ 3, 2, 2 ], [128X[104X562[4X[28X [ 3, 3, 1 ], [ 4, 1, 1, 1 ], [ 4, 2, 1 ], [ 4, 3 ], [ 5, 1, 1 ], [128X[104X563[4X[28X [ 5, 2 ], [ 6, 1 ], [ 7 ] ][128X[104X564[4X[25Xgap>[125X [27XPartitions( 8, 3 );[127X[104X565[4X[28X[ [ 3, 3, 2 ], [ 4, 2, 2 ], [ 4, 3, 1 ], [ 5, 2, 1 ], [ 6, 1, 1 ] ][128X[104X566[4X[25Xgap>[125X [27XNrPartitions( 7 );[127X[104X567[4X[28X15[128X[104X568[4X[25Xgap>[125X [27XNrPartitions( 100 );[127X[104X569[4X[28X190569292[128X[104X570[4X[32X[104X571572[33X[0;0YThe function [2XOrderedPartitions[102X ([14X16.2-21[114X) is the ordered counterpart of573[2XPartitions[102X ([14X16.2-18[114X).[133X574575[1X16.2-21 OrderedPartitions[101X576577[33X[1;0Y[29X[2XOrderedPartitions[102X( [3Xn[103X[, [3Xk[103X] ) [32X function[133X578579[33X[0;0Yreturns the set of all ordered partitions of the positive integer [3Xn[103X into580sums with [3Xk[103X summands. If [3Xk[103X is not given it returns all ordered partitions of581[3Xset[103X for all [3Xk[103X.[133X582583[33X[0;0YAn [13Xordered partition[113X is an ordered sum [22Xn = p_1 + p_2 + ... + p_k[122X of positive584integers and is represented by the list [22X[ p_1, p_2, ..., p_k ][122X. There are585totally [22X2^{n-1}[122X ordered partitions and [22X{n-1 choose k-1}[122X (see [2XBinomial[102X586([14X16.1-2[114X)) ordered partitions with [3Xk[103X summands.[133X587588[33X[0;0YDo not call [2XOrderedPartitions[102X with an [3Xn[103X much larger than [22X15[122X, the list will589simply become too large.[133X590591[1X16.2-22 NrOrderedPartitions[101X592593[33X[1;0Y[29X[2XNrOrderedPartitions[102X( [3Xn[103X[, [3Xk[103X] ) [32X function[133X594595[33X[0;0Yreturns the number of [10XOrderedPartitions([3Xset[103X[10X,[3Xk[103X[10X)[110X.[133X596597[4X[32X Example [32X[104X598[4X[25Xgap>[125X [27XOrderedPartitions( 5 );[127X[104X599[4X[28X[ [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 2 ], [ 1, 1, 2, 1 ], [ 1, 1, 3 ], [128X[104X600[4X[28X [ 1, 2, 1, 1 ], [ 1, 2, 2 ], [ 1, 3, 1 ], [ 1, 4 ], [ 2, 1, 1, 1 ], [128X[104X601[4X[28X [ 2, 1, 2 ], [ 2, 2, 1 ], [ 2, 3 ], [ 3, 1, 1 ], [ 3, 2 ], [128X[104X602[4X[28X [ 4, 1 ], [ 5 ] ][128X[104X603[4X[25Xgap>[125X [27XOrderedPartitions( 6, 3 );[127X[104X604[4X[28X[ [ 1, 1, 4 ], [ 1, 2, 3 ], [ 1, 3, 2 ], [ 1, 4, 1 ], [ 2, 1, 3 ], [128X[104X605[4X[28X [ 2, 2, 2 ], [ 2, 3, 1 ], [ 3, 1, 2 ], [ 3, 2, 1 ], [ 4, 1, 1 ] ][128X[104X606[4X[25Xgap>[125X [27XNrOrderedPartitions(20);[127X[104X607[4X[28X524288[128X[104X608[4X[32X[104X609610[33X[0;0YThe function [2XPartitions[102X ([14X16.2-18[114X) is the unordered counterpart of611[2XOrderedPartitions[102X ([14X16.2-21[114X).[133X612613[1X16.2-23 PartitionsGreatestLE[101X614615[33X[1;0Y[29X[2XPartitionsGreatestLE[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X616617[33X[0;0Yreturns the set of all (unordered) partitions of the integer [3Xn[103X having parts618less or equal to the integer [3Xm[103X.[133X619620[1X16.2-24 PartitionsGreatestEQ[101X621622[33X[1;0Y[29X[2XPartitionsGreatestEQ[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X623624[33X[0;0Yreturns the set of all (unordered) partitions of the integer [3Xn[103X having625greatest part equal to the integer [3Xm[103X.[133X626627[1X16.2-25 RestrictedPartitions[101X628629[33X[1;0Y[29X[2XRestrictedPartitions[102X( [3Xn[103X, [3Xset[103X[, [3Xk[103X] ) [32X function[133X630631[33X[0;0YIn the first form [2XRestrictedPartitions[102X returns the set of all restricted632partitions of the positive integer [3Xn[103X into sums with [3Xk[103X summands with the633summands of the partition coming from the set [3Xset[103X. If [3Xk[103X is not given all634restricted partitions for all [3Xk[103X are returned.[133X635636[33X[0;0YA [13Xrestricted partition[113X is like an ordinary partition (see [2XPartitions[102X637([14X16.2-18[114X)) an unordered sum [22Xn = p_1 + p_2 + ... + p_k[122X of positive integers638and is represented by the list [22Xp = [ p_1, p_2, ..., p_k ][122X, in nonincreasing639order. The difference is that here the [22Xp_i[122X must be elements from the set640[3Xset[103X, while for ordinary partitions they may be elements from [10X[ 1 .. n ][110X.[133X641642[1X16.2-26 NrRestrictedPartitions[101X643644[33X[1;0Y[29X[2XNrRestrictedPartitions[102X( [3Xn[103X, [3Xset[103X[, [3Xk[103X] ) [32X function[133X645646[33X[0;0Yreturns the number of [10XRestrictedPartitions([3Xn[103X[10X,[3Xset[103X[10X,[3Xk[103X[10X)[110X.[133X647648[4X[32X Example [32X[104X649[4X[25Xgap>[125X [27XRestrictedPartitions( 8, [1,3,5,7] );[127X[104X650[4X[28X[ [ 1, 1, 1, 1, 1, 1, 1, 1 ], [ 3, 1, 1, 1, 1, 1 ], [ 3, 3, 1, 1 ], [128X[104X651[4X[28X [ 5, 1, 1, 1 ], [ 5, 3 ], [ 7, 1 ] ][128X[104X652[4X[25Xgap>[125X [27XNrRestrictedPartitions(50,[1,2,5,10,20,50]);[127X[104X653[4X[28X451[128X[104X654[4X[32X[104X655656[33X[0;0YThe last example tells us that there are 451 ways to return 50 pence change657using 1, 2, 5, 10, 20 and 50 pence coins.[133X658659[1X16.2-27 SignPartition[101X660661[33X[1;0Y[29X[2XSignPartition[102X( [3Xpi[103X ) [32X function[133X662663[33X[0;0Yreturns the sign of a permutation with cycle structure [3Xpi[103X.[133X664665[33X[0;0YThis function actually describes a homomorphism from the symmetric group [22XS_n[122X666into the cyclic group of order 2, whose kernel is exactly the alternating667group [22XA_n[122X (see [2XSignPerm[102X ([14X42.4-1[114X)). Partitions of sign 1 are called [13Xeven[113X668partitions while partitions of sign [22X-1[122X are called [13Xodd[113X.[133X669670[4X[32X Example [32X[104X671[4X[25Xgap>[125X [27XSignPartition([6,5,4,3,2,1]);[127X[104X672[4X[28X-1[128X[104X673[4X[32X[104X674675[1X16.2-28 AssociatedPartition[101X676677[33X[1;0Y[29X[2XAssociatedPartition[102X( [3Xpi[103X ) [32X function[133X678679[33X[0;0Y[2XAssociatedPartition[102X returns the associated partition of the partition [3Xpi[103X680which is obtained by transposing the corresponding Young diagram.[133X681682[4X[32X Example [32X[104X683[4X[25Xgap>[125X [27XAssociatedPartition([4,2,1]);[127X[104X684[4X[28X[ 3, 2, 1, 1 ][128X[104X685[4X[25Xgap>[125X [27XAssociatedPartition([6]);[127X[104X686[4X[28X[ 1, 1, 1, 1, 1, 1 ][128X[104X687[4X[32X[104X688689[1X16.2-29 PowerPartition[101X690691[33X[1;0Y[29X[2XPowerPartition[102X( [3Xpi[103X, [3Xk[103X ) [32X function[133X692693[33X[0;0Y[2XPowerPartition[102X returns the partition corresponding to the [3Xk[103X-th power of a694permutation with cycle structure [3Xpi[103X.[133X695696[33X[0;0YEach part [22Xl[122X of [3Xpi[103X is replaced by [22Xd = gcd(l, k)[122X parts [22Xl/d[122X. So if [3Xpi[103X is a697partition of [22Xn[122X then [22X[3Xpi[103X^[3Xk[103X[122X also is a partition of [22Xn[122X. [2XPowerPartition[102X describes698the power map of symmetric groups.[133X699700[4X[32X Example [32X[104X701[4X[25Xgap>[125X [27XPowerPartition([6,5,4,3,2,1], 3);[127X[104X702[4X[28X[ 5, 4, 2, 2, 2, 2, 1, 1, 1, 1 ][128X[104X703[4X[32X[104X704705[1X16.2-30 PartitionTuples[101X706707[33X[1;0Y[29X[2XPartitionTuples[102X( [3Xn[103X, [3Xr[103X ) [32X function[133X708709[33X[0;0Y[2XPartitionTuples[102X returns the list of all [3Xr[103X-tuples of partitions which710together form a partition of [3Xn[103X.[133X711712[33X[0;0Y[3Xr[103X-tuples of partitions describe the classes and the characters of wreath713products of groups with [3Xr[103X conjugacy classes with the symmetric group [22XS_n[122X.[133X714715[1X16.2-31 NrPartitionTuples[101X716717[33X[1;0Y[29X[2XNrPartitionTuples[102X( [3Xn[103X, [3Xr[103X ) [32X function[133X718719[33X[0;0Yreturns the number of [10XPartitionTuples( [3Xn[103X[10X, [3Xr[103X[10X )[110X.[133X720721[4X[32X Example [32X[104X722[4X[25Xgap>[125X [27XPartitionTuples(3, 2);[127X[104X723[4X[28X[ [ [ 1, 1, 1 ], [ ] ], [ [ 1, 1 ], [ 1 ] ], [ [ 1 ], [ 1, 1 ] ], [128X[104X724[4X[28X [ [ ], [ 1, 1, 1 ] ], [ [ 2, 1 ], [ ] ], [ [ 1 ], [ 2 ] ], [128X[104X725[4X[28X [ [ 2 ], [ 1 ] ], [ [ ], [ 2, 1 ] ], [ [ 3 ], [ ] ], [128X[104X726[4X[28X [ [ ], [ 3 ] ] ][128X[104X727[4X[32X[104X728729730[1X16.3 [33X[0;0YFibonacci and Lucas Sequences[133X[101X731732[1X16.3-1 Fibonacci[101X733734[33X[1;0Y[29X[2XFibonacci[102X( [3Xn[103X ) [32X function[133X735736[33X[0;0Yreturns the [3Xn[103Xth number of the [13XFibonacci sequence[113X. The Fibonacci sequence [22XF_n[122X737is defined by the initial conditions [22XF_1 = F_2 = 1[122X and the recurrence738relation [22XF_{n+2} = F_{n+1} + F_n[122X. For negative [22Xn[122X we define [22XF_n = (-1)^{n+1}739F_{-n}[122X, which is consistent with the recurrence relation.[133X740741[33X[0;0YUsing generating functions one can prove that [22XF_n = ϕ^n - 1/ϕ^n[122X, where [22Xϕ[122X is742[22X(sqrt{5} + 1)/2[122X, i.e., one root of [22Xx^2 - x - 1 = 0[122X. Fibonacci numbers have743the property [22Xgcd( F_m, F_n ) = F_{gcd(m,n)}[122X. But a pair of Fibonacci numbers744requires more division steps in Euclid's algorithm (see [2XGcd[102X ([14X56.7-1[114X)) than745any other pair of integers of the same size. [10XFibonacci([3Xk[103X[10X)[110X is the special746case [10XLucas(1,-1,[3Xk[103X[10X)[1][110X (see [2XLucas[102X ([14X16.3-2[114X)).[133X747748[4X[32X Example [32X[104X749[4X[25Xgap>[125X [27XFibonacci( 10 );[127X[104X750[4X[28X55[128X[104X751[4X[25Xgap>[125X [27XFibonacci( 35 );[127X[104X752[4X[28X9227465[128X[104X753[4X[25Xgap>[125X [27XFibonacci( -10 );[127X[104X754[4X[28X-55[128X[104X755[4X[32X[104X756757[1X16.3-2 Lucas[101X758759[33X[1;0Y[29X[2XLucas[102X( [3XP[103X, [3XQ[103X, [3Xk[103X ) [32X function[133X760761[33X[0;0Yreturns the [3Xk[103X-th values of the [13XLucas sequence[113X with parameters [3XP[103X and [3XQ[103X, which762must be integers, as a list of three integers. If [3Xk[103X is a negative integer,763then the values of the Lucas sequence may be nonintegral rational numbers,764with denominator roughly [3XQ[103X^[3Xk[103X.[133X765766[33X[0;0YLet [22Xα, β[122X be the two roots of [22Xx^2 - P x + Q[122X then we define [10XLucas( [3XP[103X[10X, [3XQ[103X[10X, [3Xk[103X[10X767)[1][110X [22X= U_k = (α^k - β^k) / (α - β)[122X and [10XLucas( [3XP[103X[10X, [3XQ[103X[10X, [3Xk[103X[10X )[2][110X [22X= V_k = (α^k +768β^k)[122X and as a convenience [10XLucas( [3XP[103X[10X, [3XQ[103X[10X, [3Xk[103X[10X )[3][110X [22X= Q^k[122X.[133X769770[33X[0;0YThe following recurrence relations are easily derived from the definition771[22XU_0 = 0, U_1 = 1, U_k = P U_{k-1} - Q U_{k-2}[122X and [22XV_0 = 2, V_1 = P, V_k = P772V_{k-1} - Q V_{k-2}[122X. Those relations are actually used to define [2XLucas[102X if [22Xα773= β[122X.[133X774775[33X[0;0YAlso the more complex relations used in [2XLucas[102X can be easily derived [22XU_2k =776U_k V_k[122X, [22XU_{2k+1} = (P U_2k + V_2k) / 2[122X and [22XV_2k = V_k^2 - 2 Q^k[122X, [22XV_{2k+1} =777((P^2-4Q) U_2k + P V_2k) / 2[122X.[133X778779[33X[0;0Y[10XFibonacci([3Xk[103X[10X)[110X (see [2XFibonacci[102X ([14X16.3-1[114X)) is simply [10XLucas(1,-1,[3Xk[103X[10X)[1][110X. In an780abuse of notation, the sequence [10XLucas(1,-1,[3Xk[103X[10X)[2][110X is sometimes called the781Lucas sequence.[133X782783[4X[32X Example [32X[104X784[4X[25Xgap>[125X [27XList( [0..10], i -> Lucas(1,-2,i)[1] ); # 2^k - (-1)^k)/3[127X[104X785[4X[28X[ 0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341 ][128X[104X786[4X[25Xgap>[125X [27XList( [0..10], i -> Lucas(1,-2,i)[2] ); # 2^k + (-1)^k[127X[104X787[4X[28X[ 2, 1, 5, 7, 17, 31, 65, 127, 257, 511, 1025 ][128X[104X788[4X[25Xgap>[125X [27XList( [0..10], i -> Lucas(1,-1,i)[1] ); # Fibonacci sequence[127X[104X789[4X[28X[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ][128X[104X790[4X[25Xgap>[125X [27XList( [0..10], i -> Lucas(2,1,i)[1] ); # the roots are equal[127X[104X791[4X[28X[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ][128X[104X792[4X[32X[104X793794795[1X16.4 [33X[0;0YPermanent of a Matrix[133X[101X796797[1X16.4-1 Permanent[101X798799[33X[1;0Y[29X[2XPermanent[102X( [3Xmat[103X ) [32X function[133X800801[33X[0;0Yreturns the [13Xpermanent[113X of the matrix [3Xmat[103X. The permanent is defined by [22X∑_{p ∈802Sym(n)} ∏_{i = 1}^n mat[i][i^p][122X.[133X803804[33X[0;0YNote the similarity of the definition of the permanent to the definition of805the determinant (see [2XDeterminantMat[102X ([14X24.4-4[114X)). In fact the only difference806is the missing sign of the permutation. However the permanent is quite807unlike the determinant, for example it is not multilinear or alternating. It808has however important combinatorial properties.[133X809810[4X[32X Example [32X[104X811[4X[25Xgap>[125X [27XPermanent( [[0,1,1,1],[127X[104X812[4X[25X>[125X [27X [1,0,1,1],[127X[104X813[4X[25X>[125X [27X [1,1,0,1],[127X[104X814[4X[25X>[125X [27X [1,1,1,0]] ); # inefficient way to compute NrDerangements([1..4])[127X[104X815[4X[28X9[128X[104X816[4X[25Xgap>[125X [27X# 24 permutations fit the projective plane of order 2:[127X[104X817[4X[25Xgap>[125X [27XPermanent( [[1,1,0,1,0,0,0],[127X[104X818[4X[25X>[125X [27X [0,1,1,0,1,0,0],[127X[104X819[4X[25X>[125X [27X [0,0,1,1,0,1,0],[127X[104X820[4X[25X>[125X [27X [0,0,0,1,1,0,1],[127X[104X821[4X[25X>[125X [27X [1,0,0,0,1,1,0],[127X[104X822[4X[25X>[125X [27X [0,1,0,0,0,1,1],[127X[104X823[4X[25X>[125X [27X [1,0,1,0,0,0,1]] );[127X[104X824[4X[28X24[128X[104X825[4X[32X[104X826827828829