in reply to Combinatorial problem

Hm, not sure to understand everything that you are doing, but this is my proposed solution:
use strict; use warnings; use Data::Dumper; my $MTX = [ [4,-1,-2,-2, 0,-1], [-1,5, 0, 2,-3, 1], [-2,0, 6, 1,-3, 5], [-2,2, 1, 7,-3, 0], [0,-3,-3,-3, 8,-3], [-1,1, 5, 0,-3, 9], ]; my (@diagonal, @result); my $k = 3; for my $i (0..5) { push @diagonal, $MTX->[$i][$i]; } for my $i (0..5) { for my $j (0..5) { for my $k (0..5) { next if $i == $j or $i == $k or $j == $k; my $sc; $sc += $diagonal[$_] for ($i, $j, $k); next unless defined $sc; next if $sc < 17; push @{$result[$sc]}, [$i, $j, $k]; } } } print Dumper \@result;
The result is as follows:
$VAR18 = [ [ 0, 1, 4 ], [ 0, 2, 3 ], [ 0, 3, 2 ], [ 0, 4, 1 ], [ 1, 0, 4 ], [ 1, 4, 0 ], [ 2, 0, 3 ], [ 2, 3, 0 ], [ 3, 0, 2 ], [ 3, 2, 0 ], [ 4, 0, 1 ], [ 4, 1, 0 ] ]; $VAR19 = [ [ 0, 1, 5 ], [ 0, 2, 4 ], [ 0, 4, 2 ], [ 0, 5, 1 ], [ 1, 0, 5 ], [ 1, 2, 3 ], [ 1, 3, 2 ], [ 1, 5, 0 ], [ 2, 0, 4 ], [ 2, 1, 3 ], [ 2, 3, 1 ], [ 2, 4, 0 ], [ 3, 1, 2 ], [ 3, 2, 1 ], [ 4, 0, 2 ], [ 4, 2, 0 ], [ 5, 0, 1 ], [ 5, 1, 0 ] ]; $VAR20 = [ [ 0, 2, 5 ], [ 0, 3, 4 ], [ 0, 4, 3 ], [ 0, 5, 2 ], [ 1, 2, 4 ], [ 1, 4, 2 ], [ 2, 0, 5 ], [ 2, 1, 4 ], [ 2, 4, 1 ], [ 2, 5, 0 ], [ 3, 0, 4 ], [ 3, 4, 0 ], [ 4, 0, 3 ], [ 4, 1, 2 ], [ 4, 2, 1 ], [ 4, 3, 0 ], [ 5, 0, 2 ], [ 5, 2, 0 ] ]; $VAR21 = [ [ 0, 3, 5 ], [ 0, 5, 3 ], [ 1, 2, 5 ], [ 1, 3, 4 ], [ 1, 4, 3 ], [ 1, 5, 2 ], [ 2, 1, 5 ], [ 2, 5, 1 ], [ 3, 0, 5 ], [ 3, 1, 4 ], [ 3, 4, 1 ], [ 3, 5, 0 ], [ 4, 1, 3 ], [ 4, 3, 1 ], [ 5, 0, 3 ], [ 5, 1, 2 ], [ 5, 2, 1 ], [ 5, 3, 0 ] ]; $VAR22 = [ [ 0, 4, 5 ], [ 0, 5, 4 ], [ 1, 3, 5 ], [ 1, 5, 3 ], [ 2, 3, 4 ], [ 2, 4, 3 ], [ 3, 1, 5 ], [ 3, 2, 4 ], [ 3, 4, 2 ], [ 3, 5, 1 ], [ 4, 0, 5 ], [ 4, 2, 3 ], [ 4, 3, 2 ], [ 4, 5, 0 ], [ 5, 0, 4 ], [ 5, 1, 3 ], [ 5, 3, 1 ], [ 5, 4, 0 ] ]; $VAR23 = [ [ 1, 4, 5 ], [ 1, 5, 4 ], [ 2, 3, 5 ], [ 2, 5, 3 ], [ 3, 2, 5 ], [ 3, 5, 2 ], [ 4, 1, 5 ], [ 4, 5, 1 ], [ 5, 1, 4 ], [ 5, 2, 3 ], [ 5, 3, 2 ], [ 5, 4, 1 ] ]; $VAR24 = [ [ 2, 4, 5 ], [ 2, 5, 4 ], [ 4, 2, 5 ], [ 4, 5, 2 ], [ 5, 2, 4 ], [ 5, 4, 2 ] ]; $VAR25 = [ [ 3, 4, 5 ], [ 3, 5, 4 ], [ 4, 3, 5 ], [ 4, 5, 3 ], [ 5, 3, 4 ], [ 5, 4, 3 ] ];
I leave it up to you to check the results.

Je suis Charlie.

Replies are listed 'Best First'.
Re^2: Combinatorial problem
by hdb (Monsignor) on Apr 24, 2015 at 06:13 UTC

    You are creating a lot of duplicates. May I offer some humble adjustments:

    use strict; use warnings; my $MTX = [ [4,-1,-2,-2, 0,-1], [-1,5, 0, 2,-3, 1], [-2,0, 6, 1,-3, 5], [-2,2, 1, 7,-3, 0], [0,-3,-3,-3, 8,-3], [-1,1, 5, 0,-3, 9], ]; my @result; my @diagonal = map { $MTX->[$_][$_] } 0..5; for my $i (0..5) { for my $j ($i+1..5) { for my $k ($j+1..5) { my $sc = 0; $sc += $diagonal[$_] for ($i, $j, $k); next unless $sc > 17; push @result, [$i, $j, $k, $sc]; } } } print "@$_\n" for @result;
      You are creating a lot of duplicates.
      Yes, I know, but I had the feeling the OP wanted to keep the duplicates. Well, I still don't know what the OP really wanted, I just tried to offer something simpler.

      Je suis Charlie.