Hi BrowserUk,

I know you won't like me saying this, but I think that this comment:

It appears to me that the actual output (in the revised OP ...), is “give me all 3-tuples in the domain 0..5 such that none of the values in the tuple are duplicated.” You can construct the bit-sequences from this if you need them.
from your very dear friend sundialsvc4 is actually interesting and relevant and the idea of using a recursive function also makes some sense (although, to tell the truth, I fail to see the relevance of sundialsvc4's subsequent comments on the eight-queens problem).

So, forgetting the bits in your original question and directly searching for all unique N-tuples in a set of M unique elements, I came up with this try:

use strict; use warnings; my $M = 5; my @a = 1..$M; my $N = 3; get_all_tuples ($N, [], @a); sub get_all_tuples { my ($level, $valref, @a) = @_; if ($level <= 1) { print "@$valref $_\n" for @a; } else { while (@a) { my $i = shift @a; push @$valref, $i; get_all_tuples ($level-1, $valref, @a); pop @$valref; } } }
which produces the following output:
$ perl N_among_M.pl 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Changing M to 10 and N to 4:
$ perl N_among_M.pl 1 2 3 4 1 2 3 5 1 2 3 6 1 2 3 7 1 2 3 8 1 2 3 9 1 2 3 10 1 2 4 5 ... 5 7 9 10 5 8 9 10 6 7 8 9 6 7 8 10 6 7 9 10 6 8 9 10 7 8 9 10
Removing the printing of the solutions and just storing them in an array, and printing only the array count at the end, and using M = 20 and N = 10, the program completes in just slightly over half a second:
$ time perl N_among_M.pl 167960 real 0m0.524s user 0m0.499s sys 0m0.015s
Computing almost 1.7m solutions in just over half a second is not too bad, it seems to me, but I have no idea on the dimensions of the actual problem you are trying to solve. This solution can easily be ported to C, I think, if performance needs to be higher.

Sorry to come back relatively late on the subject, but this weekend of mine was primarily aimed at filling my tax returns. One of the most boring task each year, I am happy that I could enjoy doing some Perl programming afterwards, this is really more pleasing and more fun.


In reply to Re: Odometer pattern iterator (in C). (Updated.) by Laurent_R
in thread Odometer pattern iterator (in C). (Updated.) by BrowserUk

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.