Question: How do I get an iterator for generating k-subsets

 

The function subsets is an iterator for generating the power set of a set one set at a time.  I wanted to get iterators for all  k -subsets , so I modified the codes for getitng  iterators for the power set in the help document.

with(combinat):
k:=3:
S := subsets({1,2,3,4,a}):
while not S[finished] do
   s1:=S[nextvalue]();
 if nops(s1)=k then 
    print(s1);
    end if;
end do:

 

 

 

But I think the iterative process is done in filtering, which means that maple runs out of power sets. It may not save time.

And of course we can use "choose" to get all k-subsets at once.

choose({1,2,3,4,a}, 3)

 

So my question is is there an iterator that takes less time to get a k-subset .

I prefer the iterator to the choose function because it saves memory and makes it easy to set some breakpoints in the program to terminate the process.

Another interesting thing is that I looked at the source code when use print and it didn't seem to show.

interface(prettyprint=2):
print(combinat:-subsets)

 

 

 

 

Please Wait...