in reply to Re^3: How to determine & record all possible variations (AI::Prolog)
in thread How to determine & record all possible variations for 5 items?
could just put all of the numbers in one list. That makes the code simpler and fasterand
the first and fourth numbers are always 4 and 45, respectivelyWell that raises some interesting possibilities for even greater generality. To wit: allowing to specify the exact range of each column, individually. Here's my code, modified to do it. (I agree with you that it's rather obtuse. But it is concise. :-)
my @all_nums = 0 .. 100; my @nums = grep { !($_ % 2) or !($_ % 5) } @all_nums; # and throw some special nums in column 3: gen( [ \@nums, \@nums, [4,5], \@nums, \@all_nums, ], 100, 0, sub { print "@_\n" } ); sub gen { my( $col_ranges, $max, $used, $found, @vec ) = @_; if ( @$col_ranges == 0 ) { $found->( @vec ) if $max == $used; return; } my @col_ranges = @$col_ranges; my $col_range = shift @col_ranges; gen( \@col_ranges, $max, $used+$_, $found, @vec, $_ ) for @$col_range; }
|
|---|