# Note the digits that are not yet assigned $un0 = "0123456789"; foreach $d (split(//,$un0)) { my $un1 = $un0; $un1 =~ s/$d//; foreach $s (split(//,$un1)) { my $un2 = $un1; $un2 =~ s/$s//; foreach $t (split(//,$un2)) { # Now we can check the first constraint next unless(($d + $s)%10 == $t); my $un3 = $un2; $un3 =~ s/$t//; foreach $n (split(//,$un3)) { my $un4 = $un3; $un4 =~ s/$n//; foreach $e (split(//,$un4)) { next unless(($n.$d + $e.$s)%100 == $n.$t); . . . # Having assigned everything see if # we have the correct answer if(($j.$a.$m.$e.$s + $b.$o.$n.$d) \ == ($a.$g.$e.$n.$t)) { print "B=$b A=$a ... goto success; } } } } } } print "Failed\n"; exit(0); success: print "Solved\n"; # Warning code not checked in any way!