I swear this isn't a homework problem. I haven't attended classes in decades. Once upon a time I came up with a cute recursive formula for the partition function:
/ 0 (k > n)
f(k, n) { 1 (k = n)
\ p(k, n-k)+p(k+1, n) (k < n)
I thought to try to represent this in prolog. This is about as far as I could get:
partition(N, N, 1) :- !. %% http://stackoverflow.com/a/9582409
partition(K, N, 0) :- K > N.
partition(K, N, A+B) :-
X is K+1,
Y is N-K,
partition(X, N, A),
partition(K, Y, B).
?- partition(1, 10, X).
gives me this:
X = 1+0+0+0+0+1+(1+0+0)+(1+0+0+0+(1+0))+(1+0+0+0+1+(1+0+0)+(1+0+0+1+(1+0+1)))+(1+0+0+0+0+(1+0)+(1+0+0+1)+(1+0+0+0+(1+0)+(1+0+0+(1+0)))+(1+0+0+0+1+(1+0+0)+(1+0+0+1+(1+0+1))+(1+0+0+0+(1+0)+(1+0+0+(1+0))+(1+0+0+1+(1+0+1)+(1+0+0+(1+0)+(1+0+1+(1+0+(1+1)))))))) ?
One thing that's comforting is that there are indeed 42 ones in the above expression(?). I was hoping for X=42.
Note the question mark. Yes, there are more matches (apparently infinitely more). The second one is:
X = 1+0+0+0+0+1+(1+0+0)+(1+0+0+0+(1+0))+(1+0+0+0+1+(1+0+0)+(1+0+0+1+(1+0+1)))+(1+0+0+0+0+(1+0)+(1+0+0+1)+(1+0+0+0+(1+0)+(1+0+0+(1+0)))+(1+0+0+0+1+(1+0+0)+(1+0+0+1+(1+0+1))+(1+0+0+0+(1+0)+(1+0+0+(1+0))+(1+0+0+1+(1+0+1)+(1+0+0+(1+0)+(1+0+1+(1+(0+0)+(1+1)))))))) ?