Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Integer partitions is a very interesting topic. Creating all the partitions of a given integer is almost simple, such as the following code:

Python
def aP(n):
    """Generate partitions of n as ordered lists in ascending
    lexicographical order.

    This highly efficient routine is based on the delightful
    work of Kelleher and O'Sullivan."""

    a = [1]*n
    y = -1
    v = n
    while v > 0:
        v -= 1
        x = a[v] + 1
        while y >= 2 * x:
            a[v] = x
            y -= x
            v += 1
        w = v + 1
        while x <= y:
            a[v] = x
            a[w] = y
            yield a[:w + 1]
            x += 1
            y -= 1
        a[v] = x + y
        y = a[v] - 1
        yield a[:w]


Searching for a way to gain much more control over the function, for example in order to generate only the partitions of N size, this solution appears better:

Python
def sum_to_n(n, size, limit=None):
    """Produce all lists of `size` positive integers in decreasing order
    that add up to `n`."""
    if size == 1:
        yield [n]
        return
    if limit is None:
        limit = n
    start = (n + size - 1) // size
    stop = min(limit, n - size + 1) + 1
    for i in range(start, stop):           
        for tail in sum_to_n(n - i, size - 1, i):
            yield [i] + tail


But both of them do generates ALL the possible partitions of the given number, the first with every size, the second of the given size. What if I want only one specific partition of a given number ? The following code generates the next partition of a given partition:

Python
def next_partition(p):
  if max(p) == 1:
    return [sum(p)]
  p.sort()
  p.reverse()
  q = [ p[n] for n in range(len(p)) if p[n] > 1 ]
  q[-1] -= 1
  if (p.count(1)+1) % q[-1] == 0: 
    return q + [q[-1]]*((p.count(1)+1) // q[-1])
  else:
    return q + [q[-1]]*((p.count(1)+1) // q[-1]) + [(p.count(1)+1) % q[-1]]


But still there is a problem, you have to know what is the partition before the partition requested.

Suppose now to need a given partition of an integer N and you only know the number of the partition; example:

The partitions of 4 are:

Python
n.1 4
n.2 3+1
n.3 2+2
n.4 2+1+1
n.5 1+1+1+1


How to create the partition number 2 (3+1) giving only the integer (4), the sequence number (2) and optional the size ? All of this without the creation of all the partitions ? I have read somewhere that is possible with a mathematic formula but I do not know how.

What I have tried:

Studied a lot of textbooks about the topic but I've not reached the solution.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900