MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Partitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32852] Partitions
  • From: "Juan" <erfa11 at hotmail.com>
  • Date: Fri, 15 Feb 2002 02:49:56 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hello,
I sent this message four days ago, but it have not appeared yet in our 
MathGroup; sure I did something wrong. I try again.

If you want the partitions of n, in s parts with different numbers, here is 
a way to do it:

In[1]:= << "DiscreteMath`Combinatorica`"

In[2]:= F1[n_, s_] := Select[Partitions[n], s == Length[#1] == 
Length[Union[#1]] & ]

In[4]:= Timing[F1[50, 8]; ]
Out[4]= {77.631*Second, Null}

I am written a packade to play lottery and I need that for n = 100, 200, 
300,...
So, using the brute force philosophy, I wrote the F2  function:

In[5]:= F2[n_, s_, m_:1] :=
Module[{a = Array[p, s - 1], y, v, q, t, b = Abs[s - 2]},
   p[0] = m - 1;
   y = FoldList[Plus, 0, Range[n - 1]];
   v = Flatten[{a, n - Plus @@ a}];
   q = n - FoldList[Plus, 0, a];
    t = Table[{p[i + 1], p[i] + 1, (q[[i + 1]] - y[[s - i]])/(s - i)}, {i, 
0, s - 2}];
   Flatten[Table[v, Evaluate[Sequence @@ t]], b]]

In[6]:= Timing[F2[50, 8]; ]
Out[6]= {0.030000000000001137*Second, Null}

In[7]:=F1[15, 4]
Out[7]={{9, 3, 2, 1}, {8, 4, 2, 1}, {7, 5, 2, 1}, {7, 4, 3, 1}, {6, 5, 3, 
1}, {6, 4, 3, 2}}
In[8]:=F2[15, 4]
Out[8]={{1, 2, 3, 9}, {1, 2, 4, 8}, {1, 2, 5, 7}, {1, 3, 4, 7}, {1, 3, 5, 
6}, {2, 3, 4, 6}}

In[9]:=Length[F1[40, 7]] == Length[F2[40, 7]]
Out[9]=True

In[10]:= Timing[F2[100, 10]; ]
Out[10]= {7.170999999999992*Second, Null}


Surely F2 can be improved, or If you know something better, please send it 
here.

About the parameters of F2[n, s, m],
n and s are integers positives and s<= (Sqrt[1+8 n]-1)/2, m is a integer.

Thanks
Juan




_________________________________________________________________
Con MSN Hotmail súmese al servicio de correo electrónico más grande del 
mundo. http://www.hotmail.com/ES



  • Prev by Date: Re: Nonatomic expression ? in Append command
  • Next by Date: Re: Nonatomic expression ? in Append command
  • Previous by thread: Re: Numerical derivatives of compiled functions
  • Next by thread: Partitions