All pastes #805177 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-12-04 03:22 UTC
rendered paste body
#  reflexive transitive and antisymmetric.

my @s = 1..4;
my @p = map { $l=$_; map { "($l,$_)" } @s } @s;
my $k = @p;

my $t = 0;
for (my $i=0;$i<2**$k;$i++) {
    my %R;
    for (my $j=0;$j<$k;$j++) {
	$R{$p[$j]} = 1 if ($i & (1<<$j));
    }
    
    next unless reflexive(\%R);
    next unless transitive(\%R);
    next unless antisymmetric(\%R);
    print "{",join(",",sort keys %R),"}\n";
    $t++;
}
print "$t total\n";

sub reflexive($) {
    my $r = shift;
    foreach my $a (@s) {
	return 0 unless $r->{"($a,$a)"};
    }
    return 1;
}

sub transitive($) {
    my $r = shift;
    foreach my $a (@s) {
	foreach my $b (@s) {
	    next unless $r->{"($a,$b)"};
	    foreach my $c (@s) {
		next unless $r->{"($b,$c)"};
		return 0 unless $r->{"($a,$c)"};
	    }
	}
    }
    return 1;
}

sub antisymmetric($) {
    my $r = shift;
    foreach my $a (@s) {
	foreach my $b (@s) {
	    next unless $r->{"($a,$b)"};
	    next unless $r->{"($b,$a)"};
	    return 0 unless $a==$b;
	}
    }
    return 1;
}