MathGroup Archive 1999

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

Search the Archive

Re: combinations

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16836] Re: combinations
  • From: tobiasoed at my-dejanews.com
  • Date: Thu, 1 Apr 1999 21:35:17 -0500
  • Organization: Deja News - The Leader in Internet Discussion
  • Sender: owner-wri-mathgroup at wolfram.com

"R.B." <raulber at teleline.es> wrote
>Hi. How can i get a list of combinations for a group of 'm' elements  taken
>of 'n' in 'n', using mathematica 2.2.
>Thanks
This does the job:

TakeOneMore[result_,RestElements_]:=Module[{element},
	Map[(element=#;Map[Append[#,element]&,result])&,RestElements]
]

TakeNrecurse[n_,result_,RestElements_]:=(
	Flatten[
		MapIndexed[
			TakeNrecurse[n-1,#,Drop[RestElements,First[#2]]]&
		,
			TakeOneMore[result,Drop[RestElements,-(n-1)]]
		]
	,
		1
	]
)

TakeNrecurse[1,result_,RestElements_]:=Flatten[TakeOneMore[result,RestElements],1]

TakeN[n_,AllElements_]:=TakeNrecurse[n,{{}},AllElements]

Here is an example:

In[7]:= TakeN[3,{a,b,c,d,e}]
Out[7]=
{{a, b, c}, {a, b, d}, {a, b, e}, {a, c, d}, {a, c, e}, {a, d, e}, {b, c, d},

>   {b, c, e}, {b, d, e}, {c, d, e}}

enjoy Tobias

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


  • Prev by Date: Re: Log manipulation
  • Next by Date: Help: Debuging Functions in Mathematica
  • Previous by thread: Re: ListPlot3D
  • Next by thread: Help: Debuging Functions in Mathematica