(n, m=None, k=None, size=False)
Generate all partitions of integer n (>= 0).

    Parameters
    ==========

    ``m`` : integer (default gives partitions of all sizes)
        limits number of parts in parition (mnemonic: m, maximum parts)
    ``k`` : integer (default gives partitions number from 1 through n)
        limits the numbers that are kept in the partition (mnemonic: k, keys)
    ``size`` : bool (default False, only partition is returned)
        when ``True`` then (M, P) is returned where M is the sum of the
        multiplicities and P is the generated partition.

    Each partition is represented as a dictionary, mapping an integer
    to the number of copies of that integer in the partition.  For example,
    the first partition of 4 returned is {4: 1}, "4: one of them".

    Examples
    ========

    >>> from sympy.utilities.iterables import partitions

    The numbers appearing in the partition (the key of the returned dict)
    are limited with k:

    >>> for p in partitions(6, k=2):
    ...     print(p)
    {2: 3}
    {1: 2, 2: 2}
    {1: 4, 2: 1}
    {1: 6}

    The maximum number of parts in the partion (the sum of the values in
    the returned dict) are limited with m:

    >>> for p in partitions(6, m=2):
    ...     print(p)
    ...
    {6: 1}
    {1: 1, 5: 1}
    {2: 1, 4: 1}
    {3: 2}

    Note that the _same_ dictionary object is returned each time.
    This is for speed:  generating each partition goes quickly,
    taking constant time, independent of n.

    >>> [p for p in partitions(6, k=2)]
    [{1: 6}, {1: 6}, {1: 6}, {1: 6}]

    If you want to build a list of the returned dictionaries then
    make a copy of them:

    >>> [p.copy() for p in partitions(6, k=2)]
    [{2: 3}, {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]
    >>> [(M, p.copy()) for M, p in partitions(6, k=2, size=True)]
    [(3, {2: 3}), (4, {1: 2, 2: 2}), (5, {1: 4, 2: 1}), (6, {1: 6})]

    Reference:
        modified from Tim Peter's version to allow for k and m values:
        code.activestate.com/recipes/218332-generator-for-integer-partitions/

    See Also
    ========
    sympy.combinatorics.partitions.Partition
    sympy.combinatorics.partitions.IntegerPartition
Generate all partitions of integer n (>= 0).
Generate all partitions of integer n ( > = 0 ) .
def partitions(n, m=None, k=None, size=False):
    """Generate all partitions of integer n (>= 0).

    Parameters
    ==========

    ``m`` : integer (default gives partitions of all sizes)
        limits number of parts in parition (mnemonic: m, maximum parts)
    ``k`` : integer (default gives partitions number from 1 through n)
        limits the numbers that are kept in the partition (mnemonic: k, keys)
    ``size`` : bool (default False, only partition is returned)
        when ``True`` then (M, P) is returned where M is the sum of the
        multiplicities and P is the generated partition.

    Each partition is represented as a dictionary, mapping an integer
    to the number of copies of that integer in the partition.  For example,
    the first partition of 4 returned is {4: 1}, "4: one of them".

    Examples
    ========

    >>> from sympy.utilities.iterables import partitions

    The numbers appearing in the partition (the key of the returned dict)
    are limited with k:

    >>> for p in partitions(6, k=2):
    ...     print(p)
    {2: 3}
    {1: 2, 2: 2}
    {1: 4, 2: 1}
    {1: 6}

    The maximum number of parts in the partion (the sum of the values in
    the returned dict) are limited with m:

    >>> for p in partitions(6, m=2):
    ...     print(p)
    ...
    {6: 1}
    {1: 1, 5: 1}
    {2: 1, 4: 1}
    {3: 2}

    Note that the _same_ dictionary object is returned each time.
    This is for speed:  generating each partition goes quickly,
    taking constant time, independent of n.

    >>> [p for p in partitions(6, k=2)]
    [{1: 6}, {1: 6}, {1: 6}, {1: 6}]

    If you want to build a list of the returned dictionaries then
    make a copy of them:

    >>> [p.copy() for p in partitions(6, k=2)]
    [{2: 3}, {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]
    >>> [(M, p.copy()) for M, p in partitions(6, k=2, size=True)]
    [(3, {2: 3}), (4, {1: 2, 2: 2}), (5, {1: 4, 2: 1}), (6, {1: 6})]

    Reference:
        modified from Tim Peter's version to allow for k and m values:
        code.activestate.com/recipes/218332-generator-for-integer-partitions/

    See Also
    ========
    sympy.combinatorics.partitions.Partition
    sympy.combinatorics.partitions.IntegerPartition

    """
    if n < 0:
        raise ValueError("n must be >= 0")
    if m == 0:
        raise ValueError("m must be > 0")
    m = min(m or n, n)
    if m < 1:
        raise ValueError("maximum numbers in partition, m, must be > 0")
    k = min(k or n, n)
    if k < 1:
        raise ValueError("maximum value in partition, k, must be > 0")

    if m*k < n:
        return

    n, m, k = as_int(n), as_int(m), as_int(k)
    q, r = divmod(n, k)
    ms = {k: q}
    keys = [k]  # ms.keys(), from largest to smallest
    if r:
        ms[r] = 1
        keys.append(r)
    room = m - q - bool(r)
    if size:
        yield sum(ms.values()), ms
    else:
        yield ms

    while keys != [1]:
        # Reuse any 1's.
        if keys[-1] == 1:
            del keys[-1]
            reuse = ms.pop(1)
            room += reuse
        else:
            reuse = 0

        while 1:
            # Let i be the smallest key larger than 1.  Reuse one
            # instance of i.
            i = keys[-1]
            newcount = ms[i] = ms[i] - 1
            reuse += i
            if newcount == 0:
                del keys[-1], ms[i]
            room += 1

            # Break the remainder into pieces of size i-1.
            i -= 1
            q, r = divmod(reuse, i)
            need = q + bool(r)
            if need > room:
                if not keys:
                    return
                continue

            ms[i] = q
            keys.append(i)
            if r:
                ms[r] = 1
                keys.append(r)
            break
        room -= need
        if size:
            yield sum(ms.values()), ms
        else:
            yield ms
def partitions ( n , m = None , k = None , size = False ) : if n < 0 : raise ValueError ( "n must be >= 0" ) if m == 0 : raise ValueError ( "m must be > 0" ) m = min ( m or n , n ) if m < 1 : raise ValueError ( "maximum numbers in partition, m, must be > 0" ) k = min ( k or n , n ) if k < 1 : raise ValueError ( "maximum value in partition, k, must be > 0" ) if m * k < n : return n , m , k = as_int ( n ) , as_int ( m ) , as_int ( k ) q , r = divmod ( n , k ) ms = { k : q } keys = [ k ] # ms.keys(), from largest to smallest if r : ms [ r ] = 1 keys . append ( r ) room = m - q - bool ( r ) if size : yield sum ( ms . values ( ) ) , ms else : yield ms while keys != [ 1 ] : # Reuse any 1's. if keys [ - 1 ] == 1 : del keys [ - 1 ] reuse = ms . pop ( 1 ) room += reuse else : reuse = 0 while 1 : # Let i be the smallest key larger than 1.  Reuse one # instance of i. i = keys [ - 1 ] newcount = ms [ i ] = ms [ i ] - 1 reuse += i if newcount == 0 : del keys [ - 1 ] , ms [ i ] room += 1 # Break the remainder into pieces of size i-1. i -= 1 q , r = divmod ( reuse , i ) need = q + bool ( r ) if need > room : if not keys : return continue ms [ i ] = q keys . append ( i ) if r : ms [ r ] = 1 keys . append ( r ) break room -= need if size : yield sum ( ms . values ( ) ) , ms else : yield ms