138.
And I'm really ashamed to admit that the program I used to come up with that number is only a fraction of your initial post:
#!perl
$max = 1000000000;
@bc = (
sub { return $_[0] + $_[1]; },
sub { return $_[0] - $_[1]; },
sub { return $_[0] * $_[1]; },
sub { return (!$_[1]||$_[0]%$_[1])?-1:$_[0]/$_[1]; },
);
@bp = (
sub { return "(".$_[0]." + ".$_[1].")"; },
sub { return "(".$_[0]." - ".$_[1].")"; },
sub { return "(".$_[0]." * ".$_[1].")"; },
sub { return "(".$_[0]." / ".$_[1].")"; },
);
sub combine {
($in1, $in2, $out) = @_;
foreach $v1 (sort { $a <=> $b } keys %$in1) {
foreach $v2 (sort { $a <=> $b } keys %$in2) {
foreach $o (0..$#bc) {
$n = &{$bc[$o]}($v1, $v2);
if ($n > -1 && $n < $max && $out->{$n} eq '') {
$out->{$n} = &{$bp[$o]}($in1->{$v1}, $in2->{$v2});
}
}
}
}
}
sub iteration {
&combine($v->[$_[0]-$_], $v->[$_], $v->[$_[0]]) for (1..$_[0]-1);
}
$v->[$_] = {} for (0..9);
$v->[1]{9} = "9";
&iteration($_)for(2..9);
foreach (0..200) { print "$_ => $v->[9]{$_}\n"; }
In case you're wondering about comments, the whole thing is only a variation of the script I used for the four 4's challenge, so I removed them for this post, if you want there's also the commented version.
(Also: did I mention I
love perl?)