use strict; use warnings; use Algorithm::Combinatorics qw(combinations); my $rep = 5; # should be 100 my @data = 0..$rep; my $iter = combinations( \@data, 4 ); while( my $p = $iter->next ) { my ( $z, $y, $x, $w ) = @$p; next unless $w-2*$x+$y or $x-2*$y+$z; print "$w, $x, $y, $z\n"; }