Skip to content

Instantly share code, notes, and snippets.

@pjlsergeant
Created December 31, 2010 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjlsergeant/761378 to your computer and use it in GitHub Desktop.
Save pjlsergeant/761378 to your computer and use it in GitHub Desktop.
sub permutation {
my ( $count, $columns, $permutations ) = @_;
my $total_permutations = @$permutations ** $columns;
return if $count >= $total_permutations;
my $num = $count;
my $base = @$permutations;
my $s = '';
while (1) {
my $r = $num % $base;
$s = $permutations->[$r] . $s;
$num = int($num / $base);
last if $num == 0;
}
while ( length( $s ) < $columns ) {
$s = $permutations->[0] . $s;
}
return $s;
}
my $i=0;
while (1) {
my $p = permutation( $i++, 3, [qw( 0 3 6 9 C F )] );
if ( defined $p ) {
print "$p\n";
} else {
last;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment