Skip to content

IntegerPartitions ​

The partitions of nn into positive parts, as a lazy indexed family.

IntegerPartitions(n)every way to write nn as a sum of positive parts, order-insensitive.
CombinatoricaPartitions[n]
IntegerPartitions(n, k)the partitions of nn into at most kk parts.
CombinatoricaIf[IntegerQ[k], Reverse[Sort[Map[TransposePartition, Partitions[n, k]], Function[{a, b}, OrderedQ[{PadRight[a, n], PadRight[b, n]}]]]], If[ListQ[k] && 1 <= Length[k] <= 3, Select[Partitions[n], MemberQ[Switch[Length[k], 1, k, 2, Range[k[[1]], Min[k[[2]], n]], 3, Range[k[[1]], Min[k[[2]], n], k[[3]]]], Length[#]] &], Missing["NotCombinatorica"]]]
IntegerPartitions(n, {k})the partitions of nn into exactly kk parts.
CombinatoricaIf[IntegerQ[{k}], Reverse[Sort[Map[TransposePartition, Partitions[n, {k}]], Function[{a, b}, OrderedQ[{PadRight[a, n], PadRight[b, n]}]]]], If[ListQ[{k}] && 1 <= Length[{k}] <= 3, Select[Partitions[n], MemberQ[Switch[Length[{k}], 1, {k}, 2, Range[{k}[[1]], Min[{k}[[2]], n]], 3, Range[{k}[[1]], Min[{k}[[2]], n], {k}[[3]]]], Length[#]] &], Missing["NotCombinatorica"]]]
IntegerPartitions(n, All, parts)the partitions of nn using only parts drawn from the given list.
Typed signatures
TypePackageApplies
(integer, any?, any?) -> indexed_collection<integer_partition>combinatoricsalways

Domain: Collections

Details
  • A lazy indexed collection, unranked in reverse-lexicographic order; the count is the partition number p(n)p(n) — p(8)=22p(8) = 22 — with no elementary closed form.
  • Each element is the part list in weakly decreasing order; drawn as a Ferrers diagram, its statistics (Length, LargestPart, DurfeeSquare, …) live in @enumeratio/statistics@enumeratio/statistics.
  • Conjugation (transposing the diagram) is an involution; the self-conjugate partitions of nn equal the partitions of nn into distinct odd parts.

Definition

Epsil, as the implementation runs it.

count(_n)

_tables[_n * (_n + 1) + _n + 1]

unrank(_n, _r)

Drop(Fold(Function(
                    Apply((pw_m: integer) => Apply((pw_c: integer) => Apply((pw_r: integer) => pw_u if pw_m == 0 else Apply(
                                                                                                                             (pw_chosen: list<integer>) => Join(
                                                                                                                                                                 [pw_m - pw_chosen[2], pw_chosen[2], pw_chosen[1]],
                                                                                                                                                                 Drop(pw_u, 3),
                                                                                                                                                                 [pw_chosen[2]],
                                                                                                                                                               ),
                                                                                                                             Fold(
                                                                                                                                   Function(pw_pick if pw_pick[2] != 0 else Apply(
                                                                                                                                                                                   (pw_k: integer) => [pw_pick[1], pw_p] if pw_pick[1] < pw_k else [pw_pick[1] - pw_k, 0],
                                                                                                                                                                                   _tables[(pw_m - pw_p) *
                                                                                                                                                                                           (_n + 1) + pw_p + 1],
                                                                                                                                                                                 ) if True else pw_pick, pw_pick, pw_p),
                                                                                                                                   [pw_r, 0],
                                                                                                                                   Range(Min(pw_m, pw_c), 1, -1),
                                                                                                                                 ),
                                                                                                                           ), pw_u[3]), pw_u[2]), pw_u[1]),
                    pw_u,
                    pw_i,
                  ), [_n, _n, _r], Range(1, _n, 1)), 3)

rank(_n, _x)

Fold(Function(Apply((pw_r: integer) => Apply(
                                              (pw_m: integer) => Apply((pw_c: integer) => [pw_r +
                                                                                           Fold(
                                                                                                 Function(pw_s +
                                                                                                          (_tables[(pw_m - pw_q) *
                                                                                                                   (_n + 1) + pw_q + 1] if True else 0), pw_s, pw_q),
                                                                                                 0,
                                                                                                 Range(
                                                                                                        pw_x + 1,
                                                                                                        Min(pw_m, pw_c),
                                                                                                        1,
                                                                                                      ),
                                                                                               ), pw_m - pw_x, pw_x], pw_a[3]),
                                              pw_a[2],
                                            ), pw_a[1]), pw_a, pw_x), [0, _n, _n], _x)[1]

valid(_n, _x)

Apply(
       (pw_end: list<integer>) => pw_end[1] == 1 && pw_end[2] == 0 && 1 != 0,
       Fold(Function(
                      Apply((pw_f: integer) => Apply((pw_m: integer) => Apply(
                                                                               (pw_c: integer) => [1, pw_m - pw_y, pw_y] if pw_f == 1 &&
                                                                                                                            pw_y >= 1 &&
                                                                                                                            pw_y <=
                                                                                                                            Min(pw_m, pw_c) &&
                                                                                                                            True else [0, 0, 0],
                                                                               pw_v[3],
                                                                             ), pw_v[2]), pw_v[1]),
                      pw_v,
                      pw_y,
                    ), [1, _n, _n], _x),
     )

tables(_n)

Fold(
      Function(Join(
                     pw_t,
                     Fold(
                           Function(
                                     ReplaceAt(
                                                pw_row,
                                                pw_c + 1,
                                                Fold(
                                                      Function(pw_a +
                                                               (pw_t[(pw_s -
                                                                      pw_p) * (_n +
                                                                           1) +
                                                                     pw_p + 1] if True else 0), pw_a, pw_p),
                                                      0,
                                                      Range(
                                                             1,
                                                             Min(pw_s, pw_c),
                                                             1,
                                                           ),
                                                    ),
                                              ),
                                     pw_row,
                                     pw_c,
                                   ),
                           Map(Function(0, pw_z), Range(0, _n + 1 - 1, 1)),
                           Range(0, _n + 1 - 1, 1),
                         ),
                   ), pw_t, pw_s),
      Fold(
            Function(ReplaceAt(pw_row, pw_c + 1, 1), pw_row, pw_c),
            Map(Function(0, pw_z), Range(0, _n + 1 - 1, 1)),
            Range(0, _n + 1 - 1, 1),
          ),
      Range(1, _n + 1 - 1, 1),
    )

Statistics

On IntegerPartition, called as CombinatorialStat(x, "Name").

Maps

Into and out of IntegerPartition, called as CombinatorialMap(x, "Name"). Properties are the laws we state and what FindStat records.

From IntegerPartition

  • BinaryWordIntegerPartition → BinaryWordInjectionMp00095The shape's boundary read from the first row down: 1 for a step down, 0 for a step left.
  • BulgarianSolitaireIntegerPartition → IntegerPartitionGraded mapMp00308A move of Bulgarian solitaire: the first column of the Ferrers diagram becomes a new row.
  • GlaisherFranklinIntegerPartition → IntegerPartitionbijectioninverseBijectionGraded mapMp00312The Glaisher-Franklin bijection: even part sizes halved become repeated parts, and repeated odd parts merge into their binary multiples.
  • InitialTableauIntegerPartition → StandardTableauInjectionGraded mapMp00042The standard tableau of the shape filled row by row with 1, 2, ..., n.
  • InverseGlaisherFranklinIntegerPartition → IntegerPartitionbijectioninverseBijectionGraded mapMp00313The inverse of GlaisherFranklin: repeated parts merge into doubled parts, and a leftover part splits into copies of its odd part.
  • MinimalDyckPathIntegerPartition → DyckPathInjectionMp00043The shortest Dyck path tracing the shape of the Ferrers diagram.
  • OddPartsWordIntegerPartition → BinaryWordSurjectionMp00317The binary word marking which parts are odd, the parts taken largest first.
  • ParallelogramPolyominoDyckPathIntegerPartition → DyckPathInjectionMp00230The Dyck path of the partition read as a parallelogram polyomino, one column per part.
  • ReadingTableauIntegerPartition → StandardTableauInjectionGraded mapMp00045The recording tableau of the row reading word of the shape filled row by row.
  • RemoveFirstPartIntegerPartition → IntegerPartitionSurjectionMp00202The partition without its largest part: its first row removed.
  • TransposePartitionIntegerPartition → IntegerPartitioninvolutionBijectionGraded mapInvolutionMp00044The conjugate partition: column i of the Ferrers diagram becomes row i.

To IntegerPartition

  • BlockSizePartitionSetPartition → IntegerPartitionsurjectionSurjectionGraded mapMp00079The partition of n a set partition's block sizes form, largest first.
  • ConjugateAfterCycleTypePermutation → IntegerPartitionThe conjugate partition of the cycle type.
  • CutOutPartitionDyckPath → IntegerPartitionInjectionMp00027The partition cut out of the staircase by the path: part i is the number of downs before the (n + 1 - i)-th up step.
  • CycleTypePermutation → IntegerPartitionsurjectionSurjectionGraded mapMp00108The multiset of cycle lengths, as a partition.
  • NegativeCycleTypeSignedPermutation → IntegerPartitionSurjectionMp00169The lengths of the negative cycles, largest first: those of the underlying permutation with an odd number of negative entries.
  • PositiveCycleTypeSignedPermutation → IntegerPartitionSurjectionMp00166The lengths of the positive cycles, largest first: those of the underlying permutation with an even number of negative entries.
  • RskShapePermutation → IntegerPartitionsurjectionSurjectionGraded mapMp00060The common shape of the RSK tableaux: the row lengths, as a partition of n.
  • SortedPartitionIntegerComposition → IntegerPartitionsurjectionSurjectionGraded mapMp00040The partition a composition's parts form, sorted largest first.
  • TableauShapeStandardTableau → IntegerPartitionsurjectionSurjectionGraded mapMp00083The shape of a standard tableau: its row lengths.
21 more in FindStat, not implemented here
  • Mp00021CorePartition → IntegerPartitionGraded mapBijectionto bounded partition
  • Mp00022CorePartition → IntegerPartitionSurjectionto partition
  • Mp00037LabeledGraph → IntegerPartitionSurjectionGraded mapto partition of connected components
  • Mp00077SemistandardTableau → IntegerPartitionSurjectionGraded mapshape
  • Mp00110Posets → IntegerPartitionSurjectionGraded mapGreene-Kleitman invariant
  • Mp00179IntegerPartition → SkewPartitionInjectionGraded mapto skew partition
  • Mp00182SkewPartition → IntegerPartitionSurjectionouter shape
  • Mp00183SkewPartition → IntegerPartitionSurjectioninner shape
  • Mp00186SkewPartition → IntegerPartitionSurjectionGraded mapdominating partition
  • Mp00204Permutation → IntegerPartitionSurjectionGraded mapLLPS
  • Mp00225SemistandardTableau → IntegerPartitionSurjectionweight
  • Mp00251LabeledGraph → IntegerPartitionclique sizes
  • Mp00275LabeledGraph → IntegerPartitionSurjectionto edge-partition of connected components
  • Mp00276LabeledGraph → IntegerPartitionto edge-partition of biconnected components
  • Mp00306Posets → IntegerPartitionrowmotion cycle type
  • Mp00307Posets → IntegerPartitionpromotion cycle type
  • Mp00311PlanePartition → IntegerPartitionGraded mapto partition
  • Mp00321IntegerPartition → IntegerPartitionBijectionGraded mapInvolution2-conjugate
  • Mp00322IntegerPartition → IntegerPartitionBijectionGraded mapLoehr-Warrington
  • Mp00323IntegerPartition → IntegerPartitionBijectionGraded mapLoehr-Warrington inverse
  • Mp00332Posets → IntegerPartitionSurjectionGraded mapJordan block partition

Enumeration

Examples

Basic #

The partitions of 8 into at most 3 parts

live ↗
InIntegerPartitions(8,3)\mathrm{IntegerPartitions}(8, 3)Out[IntegerPartition⁡([8]),IntegerPartition⁡([7,1]),IntegerPartition⁡([6,2]),IntegerPartition⁡([6,1,1]),IntegerPartition⁡([5,3]),IntegerPartition⁡([5,2,1]),IntegerPartition⁡([4,4]),IntegerPartition⁡([4,3,1]),IntegerPartition⁡([4,2,2]),IntegerPartition⁡([3,3,2])]\bigl\lbrack{\operatorname{IntegerPartition}}([8]), {\operatorname{IntegerPartition}}([7, 1]), {\operatorname{IntegerPartition}}([6, 2]), {\operatorname{IntegerPartition}}([6, 1, 1]), {\operatorname{IntegerPartition}}([5, 3]), {\operatorname{IntegerPartition}}([5, 2, 1]), {\operatorname{IntegerPartition}}([4, 4]), {\operatorname{IntegerPartition}}([4, 3, 1]), {\operatorname{IntegerPartition}}([4, 2, 2]), {\operatorname{IntegerPartition}}([3, 3, 2])\bigr\rbrack

Partitions of 5 come out largest part first, ending at all ones

live ↗
InIntegerPartitions(5)\mathrm{IntegerPartitions}(5)Out[IntegerPartition⁡([5]),IntegerPartition⁡([4,1]),IntegerPartition⁡([3,2]),IntegerPartition⁡([3,1,1]),IntegerPartition⁡([2,2,1]),IntegerPartition⁡([2,1,1,1]),IntegerPartition⁡([1,1,1,1,1])]\bigl\lbrack{\operatorname{IntegerPartition}}([5]), {\operatorname{IntegerPartition}}([4, 1]), {\operatorname{IntegerPartition}}([3, 2]), {\operatorname{IntegerPartition}}([3, 1, 1]), {\operatorname{IntegerPartition}}([2, 2, 1]), {\operatorname{IntegerPartition}}([2, 1, 1, 1]), {\operatorname{IntegerPartition}}([1, 1, 1, 1, 1])\bigr\rbrack

p(6)p(6)

live ↗
InCount(IntegerPartitions(6))\mathrm{Count}(\mathrm{IntegerPartitions}(6))Out1111

the single part 66

live ↗
InIntegerPartitions(6)[1]\mathrm{IntegerPartitions}(6)[1]OutIntegerPartition⁡([6]){\operatorname{IntegerPartition}}([6])
Scope #

Exactly 3 parts

live ↗
InIntegerPartitions(8,[3])\mathrm{IntegerPartitions}(8, \bigl\lbrack3\bigr\rbrack)Out[IntegerPartition⁡([6,1,1]),IntegerPartition⁡([5,2,1]),IntegerPartition⁡([4,3,1]),IntegerPartition⁡([4,2,2]),IntegerPartition⁡([3,3,2])]\bigl\lbrack{\operatorname{IntegerPartition}}([6, 1, 1]), {\operatorname{IntegerPartition}}([5, 2, 1]), {\operatorname{IntegerPartition}}([4, 3, 1]), {\operatorname{IntegerPartition}}([4, 2, 2]), {\operatorname{IntegerPartition}}([3, 3, 2])\bigr\rbrack

Parts restricted to {1,2,5}\{1, 2, 5\}

live ↗
InIntegerPartitions(8,All,[1,2,5])\mathrm{IntegerPartitions}(8, \mathrm{All}, \bigl\lbrack1, 2, 5\bigr\rbrack)Out[IntegerPartition⁡([5,2,1]),IntegerPartition⁡([5,1,1,1]),IntegerPartition⁡([2,2,2,2]),IntegerPartition⁡([2,2,2,1,1]),IntegerPartition⁡([2,2,1,1,1,1]),IntegerPartition⁡([2,1,1,1,1,1,1]),IntegerPartition⁡([1,1,1,1,1,1,1,1])]\bigl\lbrack{\operatorname{IntegerPartition}}([5, 2, 1]), {\operatorname{IntegerPartition}}([5, 1, 1, 1]), {\operatorname{IntegerPartition}}([2, 2, 2, 2]), {\operatorname{IntegerPartition}}([2, 2, 2, 1, 1]), {\operatorname{IntegerPartition}}([2, 2, 1, 1, 1, 1]), {\operatorname{IntegerPartition}}([2, 1, 1, 1, 1, 1, 1]), {\operatorname{IntegerPartition}}([1, 1, 1, 1, 1, 1, 1, 1])\bigr\rbrack
live ↗
InIntegerPartitions(6,[2,∞,2])\mathrm{IntegerPartitions}(6, \bigl\lbrack2, ∞, 2\bigr\rbrack)Out[IntegerPartition⁡([5,1]),IntegerPartition⁡([4,2]),IntegerPartition⁡([3,3]),IntegerPartition⁡([3,1,1,1]),IntegerPartition⁡([2,2,1,1]),IntegerPartition⁡([1,1,1,1,1,1])]\bigl\lbrack{\operatorname{IntegerPartition}}([5, 1]), {\operatorname{IntegerPartition}}([4, 2]), {\operatorname{IntegerPartition}}([3, 3]), {\operatorname{IntegerPartition}}([3, 1, 1, 1]), {\operatorname{IntegerPartition}}([2, 2, 1, 1]), {\operatorname{IntegerPartition}}([1, 1, 1, 1, 1, 1])\bigr\rbrack
live ↗
InIntegerPartitions(3,10,[1,13,34])\mathrm{IntegerPartitions}(3, 10, \bigl\lbrack1, \frac{1}{3}, \frac{3}{4}\bigr\rbrack)Out[[34,34,34,34],[13,13,13,13,13,13,13,13,13],[13,13,13,13,13,13,1],[13,13,13,1,1],[1,1,1]]\bigl\lbrack\bigl\lbrack\frac{3}{4}, \frac{3}{4}, \frac{3}{4}, \frac{3}{4}\bigr\rbrack, \bigl\lbrack\frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}\bigr\rbrack, \bigl\lbrack\frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, \frac{1}{3}, 1\bigr\rbrack, \bigl\lbrack\frac{1}{3}, \frac{1}{3}, \frac{1}{3}, 1, 1\bigr\rbrack, \bigl\lbrack1, 1, 1\bigr\rbrack\bigr\rbrack
live ↗
InIntegerPartitions(5,10,[1,−1])\mathrm{IntegerPartitions}(5, 10, \bigl\lbrack1, -1\bigr\rbrack)Out[[−1,−1,1,1,1,1,1,1,1],[−1,1,1,1,1,1,1],[1,1,1,1,1]]\bigl\lbrack\bigl\lbrack-1, -1, 1, 1, 1, 1, 1, 1, 1\bigr\rbrack, \bigl\lbrack-1, 1, 1, 1, 1, 1, 1\bigr\rbrack, \bigl\lbrack1, 1, 1, 1, 1\bigr\rbrack\bigr\rbrack
Applications #

The ways to make change for a dollar from 1, 5, 10, 25 and 50 cent coins

live ↗
InCount(IntegerPartitions(100,All,[1,5,10,25,50]))\mathrm{Count}(\mathrm{IntegerPartitions}(100, \mathrm{All}, \bigl\lbrack1, 5, 10, 25, 50\bigr\rbrack))Out292292
live ↗
InIntegerPartitions(156,10,[1,5,10,25])\mathrm{IntegerPartitions}(156, 10, \bigl\lbrack1, 5, 10, 25\bigr\rbrack)Out[IntegerPartition⁡([25,25,25,25,25,25,5,1]),IntegerPartition⁡([25,25,25,25,25,10,10,10,1]),IntegerPartition⁡([25,25,25,25,25,10,10,5,5,1])]\bigl\lbrack{\operatorname{IntegerPartition}}([25, 25, 25, 25, 25, 25, 5, 1]), {\operatorname{IntegerPartition}}([25, 25, 25, 25, 25, 10, 10, 10, 1]), {\operatorname{IntegerPartition}}([25, 25, 25, 25, 25, 10, 10, 5, 5, 1])\bigr\rbrack
Properties #

The family has p(8)=22p(8) = 22 members, compute-engine's NPartition

live ↗
InNPartition(8)\mathrm{NPartition}(8)Out2222

The five partitions of 4, in reverse-lexicographic order

live ↗
InIntegerPartitions(4)\mathrm{IntegerPartitions}(4)Out[IntegerPartition⁡([4]),IntegerPartition⁡([3,1]),IntegerPartition⁡([2,2]),IntegerPartition⁡([2,1,1]),IntegerPartition⁡([1,1,1,1])]\bigl\lbrack{\operatorname{IntegerPartition}}([4]), {\operatorname{IntegerPartition}}([3, 1]), {\operatorname{IntegerPartition}}([2, 2]), {\operatorname{IntegerPartition}}([2, 1, 1]), {\operatorname{IntegerPartition}}([1, 1, 1, 1])\bigr\rbrack

There are p(10)=42p(10) = 42 partitions of 10

live ↗
InLength(IntegerPartitions(10))\mathrm{Length}(\mathrm{IntegerPartitions}(10))Out4242
live ↗
InCount(IntegerPartitions(10))\mathrm{Count}(\mathrm{IntegerPartitions}(10))Out4242
Neat examples #

Euler: partitions into odd parts are as many as partitions into distinct parts, q(10)=10q(10) = 10. See DistinctPartitions

live ↗
InCount(IntegerPartitions(10,All,[1,3,5,7,9]))\mathrm{Count}(\mathrm{IntegerPartitions}(10, \mathrm{All}, \bigl\lbrack1, 3, 5, 7, 9\bigr\rbrack))Out1010
Statistics #

partitions of 7 by largest part

live ↗
InTally(CombinatorialStat(IntegerPartitions(7),LargestPart))\mathrm{Tally}(\mathrm{CombinatorialStat}(\mathrm{IntegerPartitions}(7), \text{LargestPart}))OutTuple⁡([7,6,5,4,3,2,1],[1,1,2,3,4,3,1])\operatorname{Tuple}(\bigl\lbrack7, 6, 5, 4, 3, 2, 1\bigr\rbrack,\bigl\lbrack1, 1, 2, 3, 4, 3, 1\bigr\rbrack)

partitions of 7 by how many distinct part sizes they use

live ↗
InTally(CombinatorialStat(IntegerPartitions(7),DistinctParts))\mathrm{Tally}(\mathrm{CombinatorialStat}(\mathrm{IntegerPartitions}(7), \text{DistinctParts}))OutTuple⁡([1,2,3],[2,11,2])\operatorname{Tuple}(\bigl\lbrack1, 2, 3\bigr\rbrack,\bigl\lbrack2, 11, 2\bigr\rbrack)
About
Examples18
Checked againstWolfram Language 15 agreeCombinatorica 9 agreemathlib4 5 agree1 error
CrosswalkWikipedia ×2MathWorldWikidataBritannicaOEIS ×6Wolfram Language ×2Combinatorica ×2SageMathSymPymathlib4

Implementation

mappedcombinatoricapackages/oracle/src/mappings.ts
mappedcombinatoricapackages/oracle/src/mappings.ts

Combinatorica's Partitions[n, k] bounds the LARGEST part by k; ours (and Wolfram's) bound the number of parts, so the template transposes each partition and restores descending lexicographic order. A list {k}, {kmin, kmax} or {kmin, kmax, dk} selects the partitions of Partitions[n] by their number of parts.

mappedmathlib4packages/oracle/src/mappings.ts

Nat.Partition n enumerated (Fintype), each partition's parts sorted largest first and the lot in reverse lexicographic order, as ours lists them; the one-argument form only

See also: Count, At, Length, IntegerCompositions, SetPartitions