Woo-hoo! More Hofstadter fans!
Strangely, the 9 10 digit problem is solvable without using genetic algorithms or any randomization functions. Here's a solution that just starts with each count at 0 and feeds the last counts into the next sentence, evaluates, and repeats until the sentence is truthful:
#!/usr/bin/perl
use strict;
use warnings;
my @list = 0..9;
my %hash;
$hash{$_} = 0 for @list;
my $valid = 0;
my $test = '';
while (!$valid) {
$valid = 1;
$test = 'This sentence contains ';
$test .= "$hash{$_} ${_}s, " for @list;
$test .= '. - ';
print $test;
my %test_hash;
for (@list) {
$test_hash{$_} = eval("\$test =~ tr/$_//");
$valid = 0 if ($hash{$_} != $test_hash{$_});
$hash{$_} = $test_hash{$_};
}
($valid == 1) ? print "yes\n" : print "no\n";
}
I won't spoil this by printing the answer, but it only took 4 iterations to achieve.