Posted on Mon 08 June 2020

Perl is the best language if itâ€™s the language that you remember well enough.

My father loves math puzzles. He told me about one he was working on, and I did a quick estimate of how complex the answer space was and told him that it would be faster to have a computer try out every answer than to be clever about solving it.

Of course he wanted me to prove that, so I had to brush off Perl or Python. For some reason, Perl felt better.

The puzzle: assign the numbers 1 through 9 to letters without repetition or omission in order to solve the following equations:

- AB x C = DE
- DE + FG = HI

```
for (my $A=1; $A <= 9; $A++) {
for (my $B=1; $B <= 9; $B++) {
next if $A == $B;
for (my $C=1; $C <= 9; $C++) {
next if $B == $C;
next if $A == $C;
for (my $D=1; $D <= 9; $D++) {
next if $C == $D;
next if $B == $D;
next if $A == $D;
for (my $E=1; $E <= 9; $E++) {
next if $D == $E;
next if $C == $E;
next if $B == $E;
next if $A == $E;
for (my $F=1; $F <= 9; $F++) {
next if $E == $F;
next if $D == $F;
next if $C == $F;
next if $B == $F;
next if $A == $F;
for (my $G=1; $G <= 9; $G++) {
next if $F == $G;
next if $E == $G;
next if $D == $G;
next if $C == $G;
next if $B == $G;
next if $A == $G;
for (my $H=1; $H <= 9; $H++) {
next if $G == $H;
next if $F == $H;
next if $E == $H;
next if $D == $H;
next if $C == $H;
next if $B == $H;
next if $A == $H;
for (my $I=1; $I <= 9; $I++) {
next if $H == $I;
next if $G == $I;
next if $F == $I;
next if $E == $I;
next if $D == $I;
next if $C == $I;
next if $B == $I;
next if $A == $I;
if (((10 * $A + $B ) * $C) == (( 10* $D) + $E)) {
if ((10 * $D + $E) + (10 * $F ) + $G == (( 10 * $H ) + $I )) {
print $A.$B."x".$C."=".$D.$E." ".$D.$E."+".$F.$G."=".$H.$I."\n";
}
}
}}}}}}}}};
```

oh, right, the answer:

17x4=68

68+25=93

Takes 1.25 seconds to run on my old Macbook, and 0.74 seconds on my house server.