CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

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

Path: gap4r8 / doc / ref / chap16.txt
Views: 418346
1
2
16 Combinatorics
3
4
This chapter describes functions that deal with combinatorics. We mainly
5
concentrate on two areas. One is about selections, that is the ways one can
6
select elements from a set. The other is about partitions, that is the ways
7
one can partition a set into the union of pairwise disjoint subsets.
8
9
10
16.1 Combinatorial Numbers
11
12
16.1-1 Factorial
13
14
Factorial( n )  function
15
16
returns the factorial n! of the positive integer n, which is defined as the
17
product 1 ⋅ 2 ⋅ 3 ⋯ n.
18
19
n! is the number of permutations of a set of n elements. 1 / n! is the
20
coefficient of x^n in the formal series exp(x), which is the generating
21
function for factorial.
22
23
 Example 
24
gap> List( [0..10], Factorial );
25
[ 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 ]
26
gap> Factorial( 30 );
27
265252859812191058636308480000000
28

29
30
PermutationsList (16.2-12) computes the set of all permutations of a list.
31
32
16.1-2 Binomial
33
34
Binomial( n, k )  function
35
36
returns the binomial coefficient {n choose k} of integers n and k, which is
37
defined as n! / (k! (n-k)!) (see Factorial (16.1-1)). We define {0 choose 0}
38
= 1, {n choose k} = 0 if k < 0 or n < k, and {n choose k} = (-1)^k {-n+k-1
39
choose k} if n < 0, which is consistent with the equivalent definition {n
40
choose k} = {n-1 choose k} + {n-1 choose k-1}.
41
42
{n choose k} is the number of combinations with k elements, i.e., the number
43
of subsets with k elements, of a set with n elements. {n choose k} is the
44
coefficient of the term x^k of the polynomial (x + 1)^n, which is the
45
generating function for {n choose .}, hence the name.
46
47
 Example 
48
gap> # Knuth calls this the trademark of Binomial:
49
gap> List( [0..4], k->Binomial( 4, k ) );
50
[ 1, 4, 6, 4, 1 ]
51
gap> List( [0..6], n->List( [0..6], k->Binomial( n, k ) ) );;
52
gap> # the lower triangle is called Pascal's triangle:
53
gap> PrintArray( last );
54
[ [ 1, 0, 0, 0, 0, 0, 0 ],
55
 [ 1, 1, 0, 0, 0, 0, 0 ],
56
 [ 1, 2, 1, 0, 0, 0, 0 ],
57
 [ 1, 3, 3, 1, 0, 0, 0 ],
58
 [ 1, 4, 6, 4, 1, 0, 0 ],
59
 [ 1, 5, 10, 10, 5, 1, 0 ],
60
 [ 1, 6, 15, 20, 15, 6, 1 ] ]
61
gap> Binomial( 50, 10 );
62
10272278170
63

64
65
NrCombinations (16.2-3) is the generalization of Binomial for multisets.
66
Combinations (16.2-1) computes the set of all combinations of a multiset.
67
68
16.1-3 Bell
69
70
Bell( n )  function
71
72
returns the Bell number B(n). The Bell numbers are defined by B(0) = 1 and
73
the recurrence B(n+1) = ∑_{k = 0}^n {n choose k} B(k).
74
75
B(n) is the number of ways to partition a set of n elements into pairwise
76
disjoint nonempty subsets (see PartitionsSet (16.2-16)). This implies of
77
course that B(n) = ∑_{k = 0}^n S_2(n,k) (see Stirling2 (16.1-6)). B(n)/n! is
78
the coefficient of x^n in the formal series exp( exp(x)-1 ), which is the
79
generating function for B(n).
80
81
 Example 
82
gap> List( [0..6], n -> Bell( n ) );
83
[ 1, 1, 2, 5, 15, 52, 203 ]
84
gap> Bell( 14 );
85
190899322
86

87
88
16.1-4 Bernoulli
89
90
Bernoulli( n )  function
91
92
returns the n-th Bernoulli number B_n, which is defined by B_0 = 1 and B_n =
93
-∑_{k = 0}^{n-1} {n+1 choose k} B_k/(n+1).
94
95
B_n / n! is the coefficient of x^n in the power series of x / (exp(x)-1).
96
Except for B_1 = -1/2 the Bernoulli numbers for odd indices are zero.
97
98
 Example 
99
gap> Bernoulli( 4 );
100
-1/30
101
gap> Bernoulli( 10 );
102
5/66
103
gap> Bernoulli( 12 ); # there is no simple pattern in Bernoulli numbers
104
-691/2730
105
gap> Bernoulli( 50 ); # and they grow fairly fast
106
495057205241079648212477525/66
107

108
109
16.1-5 Stirling1
110
111
Stirling1( n, k )  function
112
113
returns the Stirling number of the first kind S_1(n,k) of the integers n and
114
k. Stirling numbers of the first kind are defined by S_1(0,0) = 1, S_1(n,0)
115
= S_1(0,k) = 0 if n, k ne 0 and the recurrence S_1(n,k) = (n-1) S_1(n-1,k) +
116
S_1(n-1,k-1).
117
118
S_1(n,k) is the number of permutations of n points with k cycles. Stirling
119
numbers of the first kind appear as coefficients in the series n! {x choose
120
n} = ∑_{k = 0}^n S_1(n,k) x^k which is the generating function for Stirling
121
numbers of the first kind. Note the similarity to x^n = ∑_{k = 0}^n S_2(n,k)
122
k! {x choose k} (see Stirling2 (16.1-6)). Also the definition of S_1 implies
123
S_1(n,k) = S_2(-k,-n) if n, k < 0. There are many formulae relating Stirling
124
numbers of the first kind to Stirling numbers of the second kind, Bell
125
numbers, and Binomial coefficients.
126
127
 Example 
128
gap> # Knuth calls this the trademark of S_1:
129
gap> List( [0..4], k -> Stirling1( 4, k ) );
130
[ 0, 6, 11, 6, 1 ]
131
gap> List( [0..6], n->List( [0..6], k->Stirling1( n, k ) ) );;
132
gap> # note the similarity with Pascal's triangle for Binomial numbers
133
gap> PrintArray( last );
134
[ [ 1, 0, 0, 0, 0, 0, 0 ],
135
 [ 0, 1, 0, 0, 0, 0, 0 ],
136
 [ 0, 1, 1, 0, 0, 0, 0 ],
137
 [ 0, 2, 3, 1, 0, 0, 0 ],
138
 [ 0, 6, 11, 6, 1, 0, 0 ],
139
 [ 0, 24, 50, 35, 10, 1, 0 ],
140
 [ 0, 120, 274, 225, 85, 15, 1 ] ]
141
gap> Stirling1(50,10);
142
101623020926367490059043797119309944043405505380503665627365376
143

144
145
16.1-6 Stirling2
146
147
Stirling2( n, k )  function
148
149
returns the Stirling number of the second kind S_2(n,k) of the integers n
150
and k. Stirling numbers of the second kind are defined by S_2(0,0) = 1,
151
S_2(n,0) = S_2(0,k) = 0 if n, k ne 0 and the recurrence S_2(n,k) = k
152
S_2(n-1,k) + S_2(n-1,k-1).
153
154
S_2(n,k) is the number of ways to partition a set of n elements into k
155
pairwise disjoint nonempty subsets (see PartitionsSet (16.2-16)). Stirling
156
numbers of the second kind appear as coefficients in the expansion of x^n =
157
∑_{k = 0}^n S_2(n,k) k! {x choose k}. Note the similarity to n! {x choose n}
158
= ∑_{k = 0}^n S_1(n,k) x^k (see Stirling1 (16.1-5)). Also the definition of
159
S_2 implies S_2(n,k) = S_1(-k,-n) if n, k < 0. There are many formulae
160
relating Stirling numbers of the second kind to Stirling numbers of the
161
first kind, Bell numbers, and Binomial coefficients.
162
163
 Example 
164
gap> # Knuth calls this the trademark of S_2:
165
gap> List( [0..4], k->Stirling2( 4, k ) );
166
[ 0, 1, 7, 6, 1 ]
167
gap> List( [0..6], n->List( [0..6], k->Stirling2( n, k ) ) );;
168
gap> # note the similarity with Pascal's triangle for Binomial numbers
169
gap> PrintArray( last );
170
[ [ 1, 0, 0, 0, 0, 0, 0 ],
171
 [ 0, 1, 0, 0, 0, 0, 0 ],
172
 [ 0, 1, 1, 0, 0, 0, 0 ],
173
 [ 0, 1, 3, 1, 0, 0, 0 ],
174
 [ 0, 1, 7, 6, 1, 0, 0 ],
175
 [ 0, 1, 15, 25, 10, 1, 0 ],
176
 [ 0, 1, 31, 90, 65, 15, 1 ] ]
177
gap> Stirling2( 50, 10 );
178
26154716515862881292012777396577993781727011
179

180
181
182
16.2 Combinations, Arrangements and Tuples
183
184
16.2-1 Combinations
185
186
Combinations( mset[, k] )  function
187
188
returns the set of all combinations of the multiset mset (a list of objects
189
which may contain the same object several times) with k elements; if k is
190
not given it returns all combinations of mset.
191
192
A combination of mset is an unordered selection without repetitions and is
193
represented by a sorted sublist of mset. If mset is a proper set, there are
194
{|mset| choose k} (see Binomial (16.1-2)) combinations with k elements, and
195
the set of all combinations is just the power set of mset, which contains
196
all subsets of mset and has cardinality 2^{|mset|}.
197
198
To loop over combinations of a larger multiset use IteratorOfCombinations
199
(16.2-2) which produces combinations one by one and may save a lot of
200
memory. Another memory efficient representation of the list of all
201
combinations is provided by EnumeratorOfCombinations (16.2-2).
202
203
204
16.2-2 Iterator and enumerator of combinations
205
206
IteratorOfCombinations( mset[, k] )  function
207
EnumeratorOfCombinations( mset )  function
208
209
IteratorOfCombinations returns an Iterator (30.8-1) for combinations (see
210
Combinations (16.2-1)) of the given multiset mset. If a non-negative integer
211
k is given as second argument then only the combinations with k entries are
212
produced, otherwise all combinations.
213
214
EnumeratorOfCombinations returns an Enumerator (30.3-2) of the given
215
multiset mset. Currently only a variant without second argument k is
216
implemented.
217
218
The ordering of combinations from these functions can be different and also
219
different from the list returned by Combinations (16.2-1).
220
221
 Example 
222
gap> m:=[1..15];; Add(m, 15);
223
gap> NrCombinations(m);
224
49152
225
gap> i := 0;; for c in Combinations(m) do i := i+1; od;
226
gap> i;
227
49152
228
gap> cm := EnumeratorOfCombinations(m);;
229
gap> cm[1000];
230
[ 1, 2, 3, 6, 7, 8, 9, 10 ]
231
gap> Position(cm, [1,13,15,15]);
232
36866
233

234
235
16.2-3 NrCombinations
236
237
NrCombinations( mset[, k] )  function
238
239
returns the number of Combinations(mset,k).
240
241
 Example 
242
gap> Combinations( [1,2,2,3] );
243
[ [ ], [ 1 ], [ 1, 2 ], [ 1, 2, 2 ], [ 1, 2, 2, 3 ], [ 1, 2, 3 ], 
244
 [ 1, 3 ], [ 2 ], [ 2, 2 ], [ 2, 2, 3 ], [ 2, 3 ], [ 3 ] ]
245
gap> # number of different hands in a game of poker:
246
gap> NrCombinations( [1..52], 5 );
247
2598960
248

249
250
The function Arrangements (16.2-4) computes ordered selections without
251
repetitions, UnorderedTuples (16.2-6) computes unordered selections with
252
repetitions, and Tuples (16.2-8) computes ordered selections with
253
repetitions.
254
255
16.2-4 Arrangements
256
257
Arrangements( mset[, k] )  function
258
259
returns the set of arrangements of the multiset mset that contain k
260
elements. If k is not given it returns all arrangements of mset.
261
262
An arrangement of mset is an ordered selection without repetitions and is
263
represented by a list that contains only elements from mset, but maybe in a
264
different order. If mset is a proper set there are |mset|! / (|mset|-k)!
265
(see Factorial (16.1-1)) arrangements with k elements.
266
267
16.2-5 NrArrangements
268
269
NrArrangements( mset[, k] )  function
270
271
returns the number of Arrangements(mset,k).
272
273
As an example of arrangements of a multiset, think of the game Scrabble.
274
Suppose you have the six characters of the word "settle" and you have to
275
make a four letter word. Then the possibilities are given by
276
277
 Example 
278
gap> Arrangements( ["s","e","t","t","l","e"], 4 );
279
[ [ "e", "e", "l", "s" ], [ "e", "e", "l", "t" ], [ "e", "e", "s", "l" ],
280
 [ "e", "e", "s", "t" ], [ "e", "e", "t", "l" ], [ "e", "e", "t", "s" ],
281
 ... 93 more possibilities ...
282
 [ "t", "t", "l", "s" ], [ "t", "t", "s", "e" ], [ "t", "t", "s", "l" ] ]
283

284
285
Can you find the five proper English words, where "lets" does not count?
286
Note that the fact that the list returned by Arrangements (16.2-4) is a
287
proper set means in this example that the possibilities are listed in the
288
same order as they appear in the dictionary.
289
290
 Example 
291
gap> NrArrangements( ["s","e","t","t","l","e"] );
292
523
293

294
295
The function Combinations (16.2-1) computes unordered selections without
296
repetitions, UnorderedTuples (16.2-6) computes unordered selections with
297
repetitions, and Tuples (16.2-8) computes ordered selections with
298
repetitions.
299
300
16.2-6 UnorderedTuples
301
302
UnorderedTuples( set, k )  function
303
304
returns the set of all unordered tuples of length k of the set set.
305
306
An unordered tuple of length k of set is an unordered selection with
307
repetitions of set and is represented by a sorted list of length k
308
containing elements from set. There are {|set| + k - 1 choose k} (see
309
Binomial (16.1-2)) such unordered tuples.
310
311
Note that the fact that UnorderedTuples returns a set implies that the last
312
index runs fastest. That means the first tuple contains the smallest element
313
from set k times, the second tuple contains the smallest element of set at
314
all positions except at the last positions, where it contains the second
315
smallest element from set and so on.
316
317
16.2-7 NrUnorderedTuples
318
319
NrUnorderedTuples( set, k )  function
320
321
returns the number of UnorderedTuples(set,k).
322
323
As an example for unordered tuples think of a poker-like game played with 5
324
dice. Then each possible hand corresponds to an unordered five-tuple from
325
the set { 1, 2, ..., 6 }.
326
327
 Example 
328
gap> NrUnorderedTuples( [1..6], 5 );
329
252
330
gap> UnorderedTuples( [1..6], 5 );
331
[ [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 1, 2 ], [ 1, 1, 1, 1, 3 ], [ 1, 1, 1, 1, 4 ],
332
 [ 1, 1, 1, 1, 5 ], [ 1, 1, 1, 1, 6 ], [ 1, 1, 1, 2, 2 ], [ 1, 1, 1, 2, 3 ],
333
 ... 100 more tuples ...
334
 [ 1, 3, 5, 5, 6 ], [ 1, 3, 5, 6, 6 ], [ 1, 3, 6, 6, 6 ], [ 1, 4, 4, 4, 4 ],
335
 ... 100 more tuples ...
336
 [ 3, 3, 5, 5, 5 ], [ 3, 3, 5, 5, 6 ], [ 3, 3, 5, 6, 6 ], [ 3, 3, 6, 6, 6 ],
337
 ... 32 more tuples ...
338
 [ 5, 5, 5, 6, 6 ], [ 5, 5, 6, 6, 6 ], [ 5, 6, 6, 6, 6 ], [ 6, 6, 6, 6, 6 ] ]
339

340
341
The function Combinations (16.2-1) computes unordered selections without
342
repetitions, Arrangements (16.2-4) computes ordered selections without
343
repetitions, and Tuples (16.2-8) computes ordered selections with
344
repetitions.
345
346
16.2-8 Tuples
347
348
Tuples( set, k )  function
349
350
returns the set of all ordered tuples of length k of the set set.
351
352
An ordered tuple of length k of set is an ordered selection with repetition
353
and is represented by a list of length k containing elements of set. There
354
are |set|^k such ordered tuples.
355
356
Note that the fact that Tuples returns a set implies that the last index
357
runs fastest. That means the first tuple contains the smallest element from
358
set k times, the second tuple contains the smallest element of set at all
359
positions except at the last positions, where it contains the second
360
smallest element from set and so on.
361
362
16.2-9 EnumeratorOfTuples
363
364
EnumeratorOfTuples( set, k )  function
365
366
This function is referred to as an example of enumerators that are defined
367
by functions but are not constructed from a domain. The result is equal to
368
that of Tuples( set, k ). However, the entries are not stored physically in
369
the list but are created/identified on demand.
370
371
16.2-10 IteratorOfTuples
372
373
IteratorOfTuples( set, k )  function
374
375
For a set set and a positive integer k, IteratorOfTuples returns an iterator
376
(see 30.8) of the set of all ordered tuples (see Tuples (16.2-8)) of length
377
k of the set set. The tuples are returned in lexicographic order.
378
379
16.2-11 NrTuples
380
381
NrTuples( set, k )  function
382
383
returns the number of Tuples(set,k).
384
385
 Example 
386
gap> Tuples( [1,2,3], 2 );
387
[ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ], 
388
 [ 3, 1 ], [ 3, 2 ], [ 3, 3 ] ]
389
gap> NrTuples( [1..10], 5 );
390
100000
391

392
393
Tuples(set,k) can also be viewed as the k-fold cartesian product of set (see
394
Cartesian (21.20-16)).
395
396
The function Combinations (16.2-1) computes unordered selections without
397
repetitions, Arrangements (16.2-4) computes ordered selections without
398
repetitions, and finally the function UnorderedTuples (16.2-6) computes
399
unordered selections with repetitions.
400
401
16.2-12 PermutationsList
402
403
PermutationsList( mset )  function
404
405
PermutationsList returns the set of permutations of the multiset mset.
406
407
A permutation is represented by a list that contains exactly the same
408
elements as mset, but possibly in different order. If mset is a proper set
409
there are |mset| ! (see Factorial (16.1-1)) such permutations. Otherwise if
410
the first elements appears k_1 times, the second element appears k_2 times
411
and so on, the number of permutations is |mset| ! / (k_1! k_2! ...), which
412
is sometimes called multinomial coefficient.
413
414
16.2-13 NrPermutationsList
415
416
NrPermutationsList( mset )  function
417
418
returns the number of PermutationsList(mset).
419
420
 Example 
421
gap> PermutationsList( [1,2,3] );
422
[ [ 1, 2, 3 ], [ 1, 3, 2 ], [ 2, 1, 3 ], [ 2, 3, 1 ], [ 3, 1, 2 ], 
423
 [ 3, 2, 1 ] ]
424
gap> PermutationsList( [1,1,2,2] );
425
[ [ 1, 1, 2, 2 ], [ 1, 2, 1, 2 ], [ 1, 2, 2, 1 ], [ 2, 1, 1, 2 ], 
426
 [ 2, 1, 2, 1 ], [ 2, 2, 1, 1 ] ]
427
gap> NrPermutationsList( [1,2,2,3,3,3,4,4,4,4] );
428
12600
429

430
431
The function Arrangements (16.2-4) is the generalization of PermutationsList
432
(16.2-12) that allows you to specify the size of the permutations.
433
Derangements (16.2-14) computes permutations that have no fixed points.
434
435
16.2-14 Derangements
436
437
Derangements( list )  function
438
439
returns the set of all derangements of the list list.
440
441
A derangement is a fixpointfree permutation of list and is represented by a
442
list that contains exactly the same elements as list, but in such an order
443
that the derangement has at no position the same element as list. If the
444
list list contains no element twice there are exactly |list|! (1/2! - 1/3! +
445
1/4! - ⋯ + (-1)^n / n!) derangements.
446
447
Note that the ratio NrPermutationsList( [ 1 .. n ] ) / NrDerangements( [ 1
448
.. n ] ), which is n! / (n! (1/2! - 1/3! + 1/4! - ⋯ + (-1)^n / n!)) is an
449
approximation for the base of the natural logarithm e = 2.7182818285...,
450
which is correct to about n digits.
451
452
16.2-15 NrDerangements
453
454
NrDerangements( list )  function
455
456
returns the number of Derangements(list).
457
458
As an example of derangements suppose that you have to send four different
459
letters to four different people. Then a derangement corresponds to a way to
460
send those letters such that no letter reaches the intended person.
461
462
 Example 
463
gap> Derangements( [1,2,3,4] );
464
[ [ 2, 1, 4, 3 ], [ 2, 3, 4, 1 ], [ 2, 4, 1, 3 ], [ 3, 1, 4, 2 ], 
465
 [ 3, 4, 1, 2 ], [ 3, 4, 2, 1 ], [ 4, 1, 2, 3 ], [ 4, 3, 1, 2 ], 
466
 [ 4, 3, 2, 1 ] ]
467
gap> NrDerangements( [1..10] );
468
1334961
469
gap> Int( 10^7*NrPermutationsList([1..10])/last );
470
27182816
471
gap> Derangements( [1,1,2,2,3,3] );
472
[ [ 2, 2, 3, 3, 1, 1 ], [ 2, 3, 1, 3, 1, 2 ], [ 2, 3, 1, 3, 2, 1 ], 
473
 [ 2, 3, 3, 1, 1, 2 ], [ 2, 3, 3, 1, 2, 1 ], [ 3, 2, 1, 3, 1, 2 ], 
474
 [ 3, 2, 1, 3, 2, 1 ], [ 3, 2, 3, 1, 1, 2 ], [ 3, 2, 3, 1, 2, 1 ], 
475
 [ 3, 3, 1, 1, 2, 2 ] ]
476
gap> NrDerangements( [1,2,2,3,3,3,4,4,4,4] );
477
338
478

479
480
The function PermutationsList (16.2-12) computes all permutations of a list.
481
482
16.2-16 PartitionsSet
483
484
PartitionsSet( set[, k] )  function
485
486
returns the set of all unordered partitions of the set set into k pairwise
487
disjoint nonempty sets. If k is not given it returns all unordered
488
partitions of set for all k.
489
490
An unordered partition of set is a set of pairwise disjoint nonempty sets
491
with union set and is represented by a sorted list of such sets. There are
492
B( |set| ) (see Bell (16.1-3)) partitions of the set set and S_2( |set|, k )
493
(see Stirling2 (16.1-6)) partitions with k elements.
494
495
16.2-17 NrPartitionsSet
496
497
NrPartitionsSet( set[, k] )  function
498
499
returns the number of PartitionsSet(set,k).
500
501
 Example 
502
gap> PartitionsSet( [1,2,3] );
503
[ [ [ 1 ], [ 2 ], [ 3 ] ], [ [ 1 ], [ 2, 3 ] ], [ [ 1, 2 ], [ 3 ] ], 
504
 [ [ 1, 2, 3 ] ], [ [ 1, 3 ], [ 2 ] ] ]
505
gap> PartitionsSet( [1,2,3,4], 2 );
506
[ [ [ 1 ], [ 2, 3, 4 ] ], [ [ 1, 2 ], [ 3, 4 ] ], 
507
 [ [ 1, 2, 3 ], [ 4 ] ], [ [ 1, 2, 4 ], [ 3 ] ], 
508
 [ [ 1, 3 ], [ 2, 4 ] ], [ [ 1, 3, 4 ], [ 2 ] ], 
509
 [ [ 1, 4 ], [ 2, 3 ] ] ]
510
gap> NrPartitionsSet( [1..6] );
511
203
512
gap> NrPartitionsSet( [1..10], 3 );
513
9330
514

515
516
Note that PartitionsSet (16.2-16) does currently not support multisets and
517
that there is currently no ordered counterpart.
518
519
16.2-18 Partitions
520
521
Partitions( n[, k] )  function
522
523
returns the set of all (unordered) partitions of the positive integer n into
524
sums with k summands. If k is not given it returns all unordered partitions
525
of set for all k.
526
527
An unordered partition is an unordered sum n = p_1 + p_2 + ⋯ + p_k of
528
positive integers and is represented by the list p = [ p_1, p_2, ..., p_k ],
529
in nonincreasing order, i.e., p_1 ≥ p_2 ≥ ... ≥ p_k. We write p ⊢ n. There
530
are approximately exp(π sqrt{2/3 n}) / (4 sqrt{3} n) such partitions, use
531
NrPartitions (16.2-20) to compute the precise number.
532
533
If you want to loop over all partitions of some larger n use the more memory
534
efficient IteratorOfPartitions (16.2-19).
535
536
It is possible to associate with every partition of the integer n a
537
conjugacy class of permutations in the symmetric group on n points and vice
538
versa. Therefore p(n) :=NrPartitions(n) is the number of conjugacy classes
539
of the symmetric group on n points.
540
541
Ramanujan found the identities p(5i+4) = 0 mod 5, p(7i+5) = 0 mod 7 and
542
p(11i+6) = 0 mod 11 and many other fascinating things about the number of
543
partitions.
544
545
16.2-19 IteratorOfPartitions
546
547
IteratorOfPartitions( n )  function
548
549
For a positive integer n, IteratorOfPartitions returns an iterator
550
(see 30.8) of the set of partitions of n (see Partitions (16.2-18)). The
551
partitions of n are returned in lexicographic order.
552
553
16.2-20 NrPartitions
554
555
NrPartitions( n[, k] )  function
556
557
returns the number of Partitions(set,k).
558
559
 Example 
560
gap> Partitions( 7 );
561
[ [ 1, 1, 1, 1, 1, 1, 1 ], [ 2, 1, 1, 1, 1, 1 ], [ 2, 2, 1, 1, 1 ], 
562
 [ 2, 2, 2, 1 ], [ 3, 1, 1, 1, 1 ], [ 3, 2, 1, 1 ], [ 3, 2, 2 ], 
563
 [ 3, 3, 1 ], [ 4, 1, 1, 1 ], [ 4, 2, 1 ], [ 4, 3 ], [ 5, 1, 1 ], 
564
 [ 5, 2 ], [ 6, 1 ], [ 7 ] ]
565
gap> Partitions( 8, 3 );
566
[ [ 3, 3, 2 ], [ 4, 2, 2 ], [ 4, 3, 1 ], [ 5, 2, 1 ], [ 6, 1, 1 ] ]
567
gap> NrPartitions( 7 );
568
15
569
gap> NrPartitions( 100 );
570
190569292
571

572
573
The function OrderedPartitions (16.2-21) is the ordered counterpart of
574
Partitions (16.2-18).
575
576
16.2-21 OrderedPartitions
577
578
OrderedPartitions( n[, k] )  function
579
580
returns the set of all ordered partitions of the positive integer n into
581
sums with k summands. If k is not given it returns all ordered partitions of
582
set for all k.
583
584
An ordered partition is an ordered sum n = p_1 + p_2 + ... + p_k of positive
585
integers and is represented by the list [ p_1, p_2, ..., p_k ]. There are
586
totally 2^{n-1} ordered partitions and {n-1 choose k-1} (see Binomial
587
(16.1-2)) ordered partitions with k summands.
588
589
Do not call OrderedPartitions with an n much larger than 15, the list will
590
simply become too large.
591
592
16.2-22 NrOrderedPartitions
593
594
NrOrderedPartitions( n[, k] )  function
595
596
returns the number of OrderedPartitions(set,k).
597
598
 Example 
599
gap> OrderedPartitions( 5 );
600
[ [ 1, 1, 1, 1, 1 ], [ 1, 1, 1, 2 ], [ 1, 1, 2, 1 ], [ 1, 1, 3 ], 
601
 [ 1, 2, 1, 1 ], [ 1, 2, 2 ], [ 1, 3, 1 ], [ 1, 4 ], [ 2, 1, 1, 1 ], 
602
 [ 2, 1, 2 ], [ 2, 2, 1 ], [ 2, 3 ], [ 3, 1, 1 ], [ 3, 2 ], 
603
 [ 4, 1 ], [ 5 ] ]
604
gap> OrderedPartitions( 6, 3 );
605
[ [ 1, 1, 4 ], [ 1, 2, 3 ], [ 1, 3, 2 ], [ 1, 4, 1 ], [ 2, 1, 3 ], 
606
 [ 2, 2, 2 ], [ 2, 3, 1 ], [ 3, 1, 2 ], [ 3, 2, 1 ], [ 4, 1, 1 ] ]
607
gap> NrOrderedPartitions(20);
608
524288
609

610
611
The function Partitions (16.2-18) is the unordered counterpart of
612
OrderedPartitions (16.2-21).
613
614
16.2-23 PartitionsGreatestLE
615
616
PartitionsGreatestLE( n, m )  function
617
618
returns the set of all (unordered) partitions of the integer n having parts
619
less or equal to the integer m.
620
621
16.2-24 PartitionsGreatestEQ
622
623
PartitionsGreatestEQ( n, m )  function
624
625
returns the set of all (unordered) partitions of the integer n having
626
greatest part equal to the integer m.
627
628
16.2-25 RestrictedPartitions
629
630
RestrictedPartitions( n, set[, k] )  function
631
632
In the first form RestrictedPartitions returns the set of all restricted
633
partitions of the positive integer n into sums with k summands with the
634
summands of the partition coming from the set set. If k is not given all
635
restricted partitions for all k are returned.
636
637
A restricted partition is like an ordinary partition (see Partitions
638
(16.2-18)) an unordered sum n = p_1 + p_2 + ... + p_k of positive integers
639
and is represented by the list p = [ p_1, p_2, ..., p_k ], in nonincreasing
640
order. The difference is that here the p_i must be elements from the set
641
set, while for ordinary partitions they may be elements from [ 1 .. n ].
642
643
16.2-26 NrRestrictedPartitions
644
645
NrRestrictedPartitions( n, set[, k] )  function
646
647
returns the number of RestrictedPartitions(n,set,k).
648
649
 Example 
650
gap> RestrictedPartitions( 8, [1,3,5,7] );
651
[ [ 1, 1, 1, 1, 1, 1, 1, 1 ], [ 3, 1, 1, 1, 1, 1 ], [ 3, 3, 1, 1 ], 
652
 [ 5, 1, 1, 1 ], [ 5, 3 ], [ 7, 1 ] ]
653
gap> NrRestrictedPartitions(50,[1,2,5,10,20,50]);
654
451
655

656
657
The last example tells us that there are 451 ways to return 50 pence change
658
using 1, 2, 5, 10, 20 and 50 pence coins.
659
660
16.2-27 SignPartition
661
662
SignPartition( pi )  function
663
664
returns the sign of a permutation with cycle structure pi.
665
666
This function actually describes a homomorphism from the symmetric group S_n
667
into the cyclic group of order 2, whose kernel is exactly the alternating
668
group A_n (see SignPerm (42.4-1)). Partitions of sign 1 are called even
669
partitions while partitions of sign -1 are called odd.
670
671
 Example 
672
gap> SignPartition([6,5,4,3,2,1]);
673
-1
674

675
676
16.2-28 AssociatedPartition
677
678
AssociatedPartition( pi )  function
679
680
AssociatedPartition returns the associated partition of the partition pi
681
which is obtained by transposing the corresponding Young diagram.
682
683
 Example 
684
gap> AssociatedPartition([4,2,1]);
685
[ 3, 2, 1, 1 ]
686
gap> AssociatedPartition([6]);
687
[ 1, 1, 1, 1, 1, 1 ]
688

689
690
16.2-29 PowerPartition
691
692
PowerPartition( pi, k )  function
693
694
PowerPartition returns the partition corresponding to the k-th power of a
695
permutation with cycle structure pi.
696
697
Each part l of pi is replaced by d = gcd(l, k) parts l/d. So if pi is a
698
partition of n then pi^k also is a partition of n. PowerPartition describes
699
the power map of symmetric groups.
700
701
 Example 
702
gap> PowerPartition([6,5,4,3,2,1], 3);
703
[ 5, 4, 2, 2, 2, 2, 1, 1, 1, 1 ]
704

705
706
16.2-30 PartitionTuples
707
708
PartitionTuples( n, r )  function
709
710
PartitionTuples returns the list of all r-tuples of partitions which
711
together form a partition of n.
712
713
r-tuples of partitions describe the classes and the characters of wreath
714
products of groups with r conjugacy classes with the symmetric group S_n.
715
716
16.2-31 NrPartitionTuples
717
718
NrPartitionTuples( n, r )  function
719
720
returns the number of PartitionTuples( n, r ).
721
722
 Example 
723
gap> PartitionTuples(3, 2);
724
[ [ [ 1, 1, 1 ], [ ] ], [ [ 1, 1 ], [ 1 ] ], [ [ 1 ], [ 1, 1 ] ], 
725
 [ [ ], [ 1, 1, 1 ] ], [ [ 2, 1 ], [ ] ], [ [ 1 ], [ 2 ] ], 
726
 [ [ 2 ], [ 1 ] ], [ [ ], [ 2, 1 ] ], [ [ 3 ], [ ] ], 
727
 [ [ ], [ 3 ] ] ]
728

729
730
731
16.3 Fibonacci and Lucas Sequences
732
733
16.3-1 Fibonacci
734
735
Fibonacci( n )  function
736
737
returns the nth number of the Fibonacci sequence. The Fibonacci sequence F_n
738
is defined by the initial conditions F_1 = F_2 = 1 and the recurrence
739
relation F_{n+2} = F_{n+1} + F_n. For negative n we define F_n = (-1)^{n+1}
740
F_{-n}, which is consistent with the recurrence relation.
741
742
Using generating functions one can prove that F_n = ϕ^n - 1/ϕ^n, where ϕ is
743
(sqrt{5} + 1)/2, i.e., one root of x^2 - x - 1 = 0. Fibonacci numbers have
744
the property gcd( F_m, F_n ) = F_{gcd(m,n)}. But a pair of Fibonacci numbers
745
requires more division steps in Euclid's algorithm (see Gcd (56.7-1)) than
746
any other pair of integers of the same size. Fibonacci(k) is the special
747
case Lucas(1,-1,k)[1] (see Lucas (16.3-2)).
748
749
 Example 
750
gap> Fibonacci( 10 );
751
55
752
gap> Fibonacci( 35 );
753
9227465
754
gap> Fibonacci( -10 );
755
-55
756

757
758
16.3-2 Lucas
759
760
Lucas( P, Q, k )  function
761
762
returns the k-th values of the Lucas sequence with parameters P and Q, which
763
must be integers, as a list of three integers. If k is a negative integer,
764
then the values of the Lucas sequence may be nonintegral rational numbers,
765
with denominator roughly Q^k.
766
767
Let α, β be the two roots of x^2 - P x + Q then we define Lucas( P, Q, k
768
)[1] = U_k = (α^k - β^k) / (α - β) and Lucas( P, Q, k )[2] = V_k = (α^k +
769
β^k) and as a convenience Lucas( P, Q, k )[3] = Q^k.
770
771
The following recurrence relations are easily derived from the definition
772
U_0 = 0, U_1 = 1, U_k = P U_{k-1} - Q U_{k-2} and V_0 = 2, V_1 = P, V_k = P
773
V_{k-1} - Q V_{k-2}. Those relations are actually used to define Lucas if α
774
= β.
775
776
Also the more complex relations used in Lucas can be easily derived U_2k =
777
U_k V_k, U_{2k+1} = (P U_2k + V_2k) / 2 and V_2k = V_k^2 - 2 Q^k, V_{2k+1} =
778
((P^2-4Q) U_2k + P V_2k) / 2.
779
780
Fibonacci(k) (see Fibonacci (16.3-1)) is simply Lucas(1,-1,k)[1]. In an
781
abuse of notation, the sequence Lucas(1,-1,k)[2] is sometimes called the
782
Lucas sequence.
783
784
 Example 
785
gap> List( [0..10], i -> Lucas(1,-2,i)[1] ); # 2^k - (-1)^k)/3
786
[ 0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341 ]
787
gap> List( [0..10], i -> Lucas(1,-2,i)[2] ); # 2^k + (-1)^k
788
[ 2, 1, 5, 7, 17, 31, 65, 127, 257, 511, 1025 ]
789
gap> List( [0..10], i -> Lucas(1,-1,i)[1] ); # Fibonacci sequence
790
[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
791
gap> List( [0..10], i -> Lucas(2,1,i)[1] ); # the roots are equal
792
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
793

794
795
796
16.4 Permanent of a Matrix
797
798
16.4-1 Permanent
799
800
Permanent( mat )  function
801
802
returns the permanent of the matrix mat. The permanent is defined by ∑_{p ∈
803
Sym(n)} ∏_{i = 1}^n mat[i][i^p].
804
805
Note the similarity of the definition of the permanent to the definition of
806
the determinant (see DeterminantMat (24.4-4)). In fact the only difference
807
is the missing sign of the permutation. However the permanent is quite
808
unlike the determinant, for example it is not multilinear or alternating. It
809
has however important combinatorial properties.
810
811
 Example 
812
gap> Permanent( [[0,1,1,1],
813
>  [1,0,1,1],
814
>  [1,1,0,1],
815
>  [1,1,1,0]] ); # inefficient way to compute NrDerangements([1..4])
816
9
817
gap> # 24 permutations fit the projective plane of order 2:
818
gap> Permanent( [[1,1,0,1,0,0,0],
819
>  [0,1,1,0,1,0,0],
820
>  [0,0,1,1,0,1,0],
821
>  [0,0,0,1,1,0,1],
822
>  [1,0,0,0,1,1,0],
823
>  [0,1,0,0,0,1,1],
824
>  [1,0,1,0,0,0,1]] );
825
24
826

827
828
829