## Partitions

### May 11, 2012

The partitions of an integer is the set of all sets of integers that sum to the given integer. For instance, the partitions of 4 is the set of sets ((1 1 1 1) (1 1 2) (1 3) (2 2) (4)). We computed the number of partitions of an integer in a previous exercise. In today’s exercise, we will make a list of the partitions.

The process is recursive. There is a single partition of 0, the empty set (). There is a single partition of 1, the set (1). There are two partitions of 2, the sets (1 1) and (2). There are three partitions of 3, the sets (1 1 1), (1 2) and (3). There are five partitions of 4, the sets (1 1 1 1), (1 1 2), (1 3), (2 2), and (4). There are seven partitions of 5, the sets (1 1 1 1 1), (1 1 1 2), (1 2 2), (1 1 3), (1 4), (2 3) and (5). And so on. In each case, the next-larger set of partitions is determined by adding each integer *x* less than or equal to the desired integer *n* to all the sets formed by the partition of *n* − *x*, eliminating any duplicates.

Your task is to write a function that generates the set of all partitions of a given integer. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

A recursive solution in Clojure. The result is in the reverse order than what is shown in the problem description, because it’s slightly more convenient to check the subtrahend against 0 than against the original minuend, which would have to be remembered somehow. It shouldn’t matter as the problem calls for a set.

A copy-paste blunder. The code should read:

[sourceoode=”css”]

(defn- parts-helper [num subtr acc]

(cond

(= num 0) [acc]

(or (= subtr 0) (< num 0)) []

:else (concat

(parts-helper (- num subtr) subtr (conj acc subtr))

(parts-helper num (dec subtr) acc))))

(defn parts [num]

(parts-helper num num []))

[/sourcecode]

[…] today’s Programming Praxis exercise, our goal is to write a function that gives all unique sums that […]

My Haskell solution (see http://bonsaicode.wordpress.com/2012/05/11/programming-praxis-partitions/ for a version with comments):

Python 2.7

It strikes me that many recursive solutions will probably be inefficient

(except for Haskell ones, perhaps), since solutions to subproblems are

recomputed every time they’re needed, similar to SICP’s discussion of Fibonacci

number generation.

First up, a memoizing version (based on a Python version that Programming

Praxis pointed me to on StackOverflow) that stores previous answers.

Next are

`zs1`

and`zs2`

, Python versions of twoalgorithms found in

`Fast Algorithms for Generating Integer`

by Zoghbi and Stojmenovic (1998). They’re iterative and moreParitions

efficient than the first solution, if uglier. I’ve made them generators instead

of lists, but that’s not really relevant to the algorithms.

@Graham: The Haskell version isn’t memoized. Memoizing the function is pretty simple and doesn’t add too much to the program length though:

@Remco: thanks for that. I thought there might have been some sort of wizardry hidden in the lazy evaluation or something, but I appreciate the enlightenment.

Find partitions where the least term is equal to or greater than another parameter. This way duplicates are not generated. All partitions are found by setting the other parameter to 1.

Solution in Go:

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

partition.go

hosted with ❤ by GitHub

This shows off some of the ugly things in Go: no built-in set type (use a map with an empty value), no easy way to get all the keys for a map (have to manually copy to a slice), no way to directly use a slice as the key for a map or define a custom hashcode or equals on a type (convert the slice to a string, and use that as the key in the map).

My solution is not as neat and elegant as yours but all the same, I’m proud that I was able to do it.

Here’s my solution in Python:

def partitions(n):

p = []

# n is not 0

if n:

# from 1 to n

for i in range(1, n+1):

a = partitions(n-i)

if len(a) == 0:

p.append(i)

else:

for j in a:

if isinstance(j, int):

b = [j, i]

b.sort()

if b not in p:

p.append(b)

else:

j.append(i)

j.sort()

if j not in p:

p.append(j)

return p

# n is 0

else:

return []

Another Python version [1].

It uses a sparse representation for the partitions, e.g., 4 = 4 x 1 = 2 x 1 + 2 = 1 x 1 + 3 = 2 x 2 = 1 x 4 instead of 4 = 1+1+1+1 = 1+1+2 = 1+3 = 2 + 2 = 4.

[1] http://code.activestate.com/recipes/221132/