In essence this problem requires that you generate the subsets of the original set. Say you have the set (1,2,3,4). You want to find all these subsets

1 1 2 1 3 1 4 1 2 3 1 2 4 1 3 4 1 2 3 4 2 2 3 2 4 2 3 4 3 3 4 4
One neat way to get all the combinations is to use a binary approach where the presence of absence of a binary 1 is used to represent "grab this element" from the set. The number of possible combinations is (2**n)-1. It is easier to see in code:
my @list = (1,2,3,4); my $n = 2**@list -1; for $b(1..$n) { printf "%2d: %04b ", $b, $b; my $i = $b; for (@list) { print "$_ " if $i%2; $i >>= 1; } print "\n"; } __DATA__ 1: 0001 1 (get element 0) 2: 0010 2 (get element 1) 3: 0011 1 2 (get elements 0 and 1) 4: 0100 3 (get element 2) 5: 0101 1 3 (etc) 6: 0110 2 3 7: 0111 1 2 3 8: 1000 4 9: 1001 1 4 10: 1010 2 4 11: 1011 1 2 4 12: 1100 3 4 13: 1101 1 3 4 14: 1110 2 3 4 15: 1111 1 2 3 4

In reply to Re: Recursion problem by tachyon-II
in thread Recursion problem by someone202

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.