Emmanuel ROYER on Thu, 01 Aug 2024 16:49:19 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Enumeration of partitions |
Dear Pari users! There are two ways of representing a partition of an integer n, either as (a_1,...,a_q) where n=a_1+...+a_q or in the form (1^{b_1},...,n^{b_n}) with n=b_1+2b_2+...+nb_n. Unless I'm mistaken, partitions(n) returns the partitions in the first form. How can we translate the result into the second form as efficiently as possible? (Related to: how to count multiplicities in an ordered vector) For the moment, I'm using the following scripts, which don't seem very otpimal (the second is even longer than the first...) Partitions(n)={ my(P=partitions(n),Part=List(),L=List()); for(i=1,#P, p=P[i]; L=List(); for(j=1,n,my(S=0); for(k=j,#p,if(p[k]==j,S+=1)); listput(L,S) ); listput(Part,Vecsmall(L)); ); Vec(Part) }; \\....... Partitions3(n)={ my(P=partitions(n),Part=List(),L=List()); for(i=1,#P, p=P[i]; L=List(); for(j=1,n,my(S=0); for(k=j,#p,if(p[k]==j,S+=1,if(p[k]>j,break(1)))); listput(L,S) ); listput(Part,Vecsmall(L)); ); Vec(Part) }; ? Partitions(57); cpu time = 30,995 ms, real time = 30,995 ms. ? Partitions3(57); cpu time = 38,106 ms, real time = 38,106 ms. Best regards, Emmanuel