Re: Re: Intersection and element counts
- To: mathgroup at smc.vnet.net
- Subject: [mg20795] Re: [mg20771] Re: [mg20615] Intersection and element counts
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Sun, 14 Nov 1999 18:13:47 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
You can easily make it work with numbers too: In[14]:= intersect[ list1_, list2_ ] := Flatten[Apply[List, PolynomialGCD[ Times @@ Map[Hold, list1], Times @@ Map[Hold, list2] ]] /. Power[x_, n_] :> Table[x, {n}]] // ReleaseHold Now, with In[15]:= list1 = {a, 1, 1, a, 2, 1, 1, 1, 2}; In[16]:= list2 = {a, b, d, 1, 1, a, d, 2, 2}; In[17]:= intersect[list1, list2] Out[17]= {1, 1, 2, 2, a, a} > From: "Simons, F.H." <F.H.Simons at tue.nl> To: mathgroup at smc.vnet.net > Date: Thu, 11 Nov 1999 00:22:46 -0500 > To: mathgroup at smc.vnet.net > Subject: [mg20795] [mg20771] Re: [mg20615] Intersection and element counts > > Just for fun: the following solution works very fast when the two lists do > not contain numbers. > > intersect[ list1_, list2_ ] := PolynomialGCD[ Times @@ list1, Times @@ list2 > ] > > With > > list1 = {a, b, b, a, c, d, b, b, c}; > list2 = {a, b, d, b, b, a, d, c, c}; > > we obtain > > a^2 b^3 c^2 d. > > By applying List, followed by the simple substitution: Power[x_, n_] :> > Table[ x, {n}] and Flattening, the result may be given in the form of a > list, if desired. > > > Fred Simons > Eindhoven University of Technology >