## Partition Numbers

### April 15, 2011

This exercise is trickier than it looks. The problem is that the recursive calls to *P*(*n*) make the naive implementation take exponential time:

`(define (partition n) ; naive version`

(if (negative? n) 0

(if (zero? n) 1

(let loop ((k 1) (sum 0))

(if (< n k) sum

(loop (+ k 1)

(+ sum (* (expt -1 (+ k 1))

(+ (partition (- n (* k (- (* 3 k) 1) 1/2)))

(partition (- n (* k (+ (* 3 k) 1) 1/2))))))))))))

To calculate *P*(1000), we need to memoize the function by storing previously-computed values of the function. There are many ways to store those values, including hash tables, treaps, red-black trees, a simple, unbalanced binary search tree, a cuckoo hash, and functional arrays; for variety, we choose a *growing arrays* approach that keeps locally, inside the function closure, a vector that doubles in length whenever it becomes too small. Although it looks expensive to copy vector elements, timing tests show that it seems to work well enough, since the number of doublings is small:

`(define partition ; memoized version`

(let ((len 1) (pv (vector 1)))

(lambda (n)

(do () ((< n len))

(let* ((new-len (+ len len)) (new-pv (make-vector new-len #f)))

(do ((i 0 (+ i 1))) ((= i len))

(vector-set! new-pv i (vector-ref pv i)))

(set! len new-len) (set! pv new-pv)))

(cond ((negative? n) 0) ((zero? n) 1)

((and (< n len) (vector-ref pv n)) => (lambda (x) x))

(else (let loop ((k 1) (sum 0))

(if (< n k) (begin (vector-set! pv n sum) sum)

(loop (+ k 1) (+ sum (* (expt -1 (+ k 1))

(+ (partition (- n (* k (- (* 3 k) 1) 1/2)))

(partition (- n (* k (+ (* 3 k) 1) 1/2))))))))))))))

The line `(do () ((< n len)) (grow))`

is the standard Scheme idiom for a `while`

loop. The body of the `while`

performs the growing function by creating a new vector, copying elements one-by-one from the old vector to the new vector, then updating `len`

and `pv`

to point to the new vector. The cond clause `(and (< n len) (vector-ref pv n))`

simultaneously determines if the partition number has already been calculated (because the vector is filled with `#f`

when it is initialized) and returns its value if it has; the `=>`

operator passes on the value as the single argument of a local function, which in this case is the identity function `(lambda (x) x)`

that passes on its input to its output. Here is `partition`

in action:

`> (partition 1000)`

24061467864032622473692149727991

You can run the program at http://programmingpraxis.codepad.org/YO13k0sw.

My Haskell solution (see http://bonsaicode.wordpress.com/2011/04/15/programming-praxis-partition-numbers/ for a version with comments):

[…] today’s Programming Praxis exercise, our goal is to write a function to calculate partition numbers and to […]

A Python solution:

My non-recursive solution in Python:

My Python solution, similar to Dave Webb’s:

A version that runs faster on my laptop, based on a formula given on the Mathworld page:

Here’s a ruby version. The PN array is used for caching. I actually tried using inject, but couldn’t get the caching working with it. I’d love to see someone else’s version with that in there.

Also @Dave’s python version memoizing the function is pretty cool. I haven’t tried anything like that in Ruby. Anyone know if it’s doable?

Here’s my Ruby implementation:

$partitions = [1,1,2,3,5,7,11,15,22,30,42]

def partition(n)

return 0 if n < 0

return $partitions[n] if $partitions[n] != nil

partition = 0

1.upto(n) do |k|

partition += (-1) ** (k + 1) * (partition(n – (k * (3 * k – 1)) / 2) + partition(n – (k * (3 * k + 1)) / 2))

end

$partitions[n] = partition

return $partitions[n]

end

p partition(1000)

Here’s the output:

24061467864032622473692149727991

I was inspired by @Remco’s slick Haskell solutions to try my own. After little

luck with IntMaps (perhaps due to overflow?) I came up with the following. Note:

I’m indebted to @Remco for nearly all of this, either in this exercise or

previous ones.

inspired by you graham

Straight Scheme with delayed and forced promises.

(The promise at 0 remains an empty sum. The rest are good.)

Hey there! New to the site, but already loving it. Here’s my Python solution. Much like the above, with one VERY BIG time saving feature. (I’m not sure if the “regular crowd” here is in to optimizations… but this one seemed worthy of note.)

@memoize

def partition_number_opt1(n):

if n < 0: return 0

if n == 0: return 1

sum = 0

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

# slight optimization:

# once the inner expressions get below zero, then all remaining

# partition numbers (in the rest of the sum) are zero,

# thus they contribute nothing to the sum.

# so: break out of the loop

if n-(k*(3*k-1)//2) < 0: break

sum += ((-1) ** (k+1)) * (partition_number_opt1(n-(k*(3*k-1)//2))+partition_number_opt1(n-(k*(3*k+1)//2)))

return sum

Just trying my post once again, after I “read the manual”. Mea culpa!

Using Dan’s observation as an excuse for a little refactoring that I wished

I did in the first place: making sum a procedure of its own. And yes, it

does feel faster with the early completion of the sums.

The recursive solution is very slow because redundant calculation of partition numbers.

So, The trick is to remember the previously calculated value of partition numbers.

Checkout my implementation in C using red-black trees.

http://codepad.org/vKEivBGq

Here is my non-recursive python solution, simple and efficient!

Since Euler’s formula is complex for calculation, I used another algorithm,

instead, which can easily be discovered from the process of partition.

I found this quite interesting, as there is an enormous range of performance between different solutions. The Perl version below is combinatorial and, at least for me, is quite a bit faster and less memory than the previously shown ones (barring the C double solution which isn’t correct for large values). However it is about 300 times slower than the GMP combinatorial solution below. In turn, that is about 1,000 times slower than the Rademacher formula used in Pari or SymPy. Pari in turn is quite a bit slower than Jonathan Bober’s MPFR Rademacher solution. The current state of the art implementation seems to be Fredrik Johansson’s FLINT/MPFR/Arb implementation which looks to be over 1000x faster than Pari, computing p(10^12) in under 12s. The growth rates differ (especially between the combinatorial and Rademacher solutions), so the “x faster” is simplistic.

In Perl, computes p(10000) in about 8s.

In C+GMP, computes p(200000) in about 5s. Compile with “gcc -O partitions.c -lgmp -lm”.

Here is a short Haskell solution that implicitly memoizes without using any non-Prelude data structures, but achieves quite good performances:

integerPartitions :: [Integer]

integerPartitions =

let

p = go (let kg k = (2*k):k:kg (k+1) in 0:kg 1) [] 0

go :: [Int] -> [[Integer]] -> Int -> [Integer]

go ks a !c = s:(if c>0 then go ks b (c-1) else (let (d:kt)=ks in go kt b d))

where

(s,b) = sf a

sf ((a1:t1):(a2:t2):r) = let (t,tr)=sf r in (a1+a2-t,t1:t2:tr)

sf ((a1:t1):_) = if c>0 then (a1,t1:[]) else (a1+1,t1:p:[])

sf _ = if c>0 then (0,[]) else (1,p:[])

in

1:p

Forgive me for screwing up the formatting in the previous post. This is better, I hope: http://codepad.org/wprw8CLb . FWIW, this code calculates integerPartitions!!100000 in a little over 10 seconds on my ancient workstation.