Skip to main content
added 6 characters in body
Source Link
dcsohl
  • 651
  • 4
  • 6

Construct a square matrix where each cell (m,n) reflects the number of partitions of m whose largest number is n, according to the Knuth extract @feersum so kindly cited. For example, 5,2 gives us 2 because there are two valid partitions 42,2,1 and 3,2,1,1,1. 6,3 gives us 3 for 3,1,1,1,3,2,1 and 3,3.

Construct a square matrix where each cell (m,n) reflects the number of partitions of m whose largest number is n, according to the Knuth extract @feersum so kindly cited. For example, 5,2 gives us 2 because there are two valid partitions 4,1 and 3,2. 6,3 gives us 3 for 3,1,1,1,3,2,1 and 3,3.

Construct a square matrix where each cell (m,n) reflects the number of partitions of m whose largest number is n, according to the Knuth extract @feersum so kindly cited. For example, 5,2 gives us 2 because there are two valid partitions 2,2,1 and 2,1,1,1. 6,3 gives us 3 for 3,1,1,1,3,2,1 and 3,3.

Source Link
dcsohl
  • 651
  • 4
  • 6

Octave, 200

function r=c(m)r=[];a=eye(m);a(:,1)=1;for(i=3:m)for(j=2:i-1)a(i,j)=a(i-1,j-1)+a(i-j,j);end;end;p=randi(sum(a(m,:)));while(m>0)b=a(m,:);c=cumsum(b);x=min(find(c>=p));r=[r x];p=p-c(x)+b(x);m=m-x;end;end

Ungolfed:

function r=c(m)
  r=[];
  a=eye(m);
  a(:,1)=1;
  for(i=3:m)
    for(j=2:i-1)
      a(i,j)=a(i-1,j-1)+a(i-j,j);
    end;
  end;
  p=randi(sum(a(m,:)));
  while(m>0)
    b=a(m,:);
    c=cumsum(b);
    x=min(find(cumsum(b)>=p));
    r=[r x];
    p=p-c(x)+b(x);
    m=m-x;
  end
end

Construct a square matrix where each cell (m,n) reflects the number of partitions of m whose largest number is n, according to the Knuth extract @feersum so kindly cited. For example, 5,2 gives us 2 because there are two valid partitions 4,1 and 3,2. 6,3 gives us 3 for 3,1,1,1,3,2,1 and 3,3.

We can now deterministically find the p'th partition. Here, we are generating p as a random number but you can alter the script slightly so p is a parameter:

function r=c(m,p)
  r=[];
  a=eye(m);
  a(:,1)=1;
  for(i=3:m)
    for(j=2:i-1)
      a(i,j)=a(i-1,j-1)+a(i-j,j);
    end;
  end;
  while(m>0)
    b=a(m,1:m);
    c=cumsum(b);
    x=min(find(c>=p));
    r=[r x];
    p=p-c(x)+b(x);
    m=m-x;
  end
end

We can now deterministically show that each outcome is solely dependent on p:

octave:99> for(i=1:7)
> c(5,i)
> end
ans =

   1   1   1   1   1

ans =

   2   1   1   1

ans =

   2   2   1

ans =

   3   1   1

ans =

   3   2

ans =

   4   1

ans =  5

Thus, going back to the original where p is randomly generated, we can be assured each outcome is equally likely.