use warnings;
use strict;
my @digits_used;
#$digits_used = [false x 10];
find_digits(1, 0); #// get the ball rolling
sub find_digits {
my ($depth, $total) = @_;
for my $digit (0..9) {
if( not $digits_used[$digit]) {
$digits_used[$digit] = 1;
if ($depth < 4) {
find_digits($depth+1, ($total+$digit)*10); #// recur
+se
} else {
print $total+$digit,"\n";
}
$digits_used[$digit] = 0;
} #// if used
} #// for digits
} #// end sub
__END__
c:\usr\scripts>sundial.pl | find /c /v ""
5040
I've always been fascinated with recursive code but there are times when it isn't a good fit for a problem. Those times are any time there is a simpler solution. To produce the 5040 permutations without repeats you only need to eliminate duplicate digits from 0..9999. This can be done with a simple regex and sprintf on a Perl one liner. This looks like a good start for a competition for code obfuscation. Where did you find this code? |