There's more than one way to do things PerlMonks

### Re: list of four digit lock combinations without repeated digits

by sundialsvc4 (Abbot)
 on Jun 21, 2018 at 22:28 UTC ( #1217140=note: print w/replies, xml ) Need Help??

A simple recursive algorithm also works.   In pseudo-code ...

```   \$digits_used = [false x 10];
find_digits(1, 0);  // get the ball rolling

sub find_digits(\$depth, \$total) {
for \$digit (0..9) {
if(!digits_used[\$digit]) {
\$digits_used[\$digit] = true;
if (\$depth < 4) {
find_digits(\$depth+1, (\$total+\$digit)*10);   // recurse
} else {
print \$total+\$digit;
}
\$digits_used[\$digit] = false;
} // if used
} // for digits
} // end sub

Replies are listed 'Best First'.
Re^2: list of four digit lock combinations without repeated digits
by Lotus1 (Priest) on Jun 22, 2018 at 14:11 UTC

I converted this pseudo code to Perl and found that it produces the 5040 permutations.

```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?

"... pseudo code..."

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex(\$ENV{KARL});'Help

Create A New User
Node Status?
node history
Node Type: note [id://1217140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2019-05-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you enjoy 3D movies?

Results (152 votes). Check out past polls.

Notices?