diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-04-05 18:53:43 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-04-05 18:53:43 -0500 |
| commit | 04e54a90f6ccf94b8fbf126e7dfdef647fc2d78c (patch) | |
| tree | 408458183f53685d32ab4ac575f69bf393414353 | |
| parent | abe2973b4270201be719ea18ea83cd442adaa0e6 (diff) | |
| download | perlweeklychallenge-club-04e54a90f6ccf94b8fbf126e7dfdef647fc2d78c.tar.gz perlweeklychallenge-club-04e54a90f6ccf94b8fbf126e7dfdef647fc2d78c.tar.bz2 perlweeklychallenge-club-04e54a90f6ccf94b8fbf126e7dfdef647fc2d78c.zip | |
Optimize slightly ch-1.pl
| -rwxr-xr-x | challenge-107/wlmb/perl/ch-1.pl | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/challenge-107/wlmb/perl/ch-1.pl b/challenge-107/wlmb/perl/ch-1.pl index 3466ea1f98..7e60c30114 100755 --- a/challenge-107/wlmb/perl/ch-1.pl +++ b/challenge-107/wlmb/perl/ch-1.pl @@ -16,30 +16,34 @@ exit unless $howmany > 0; my @results; my $largest_base=36; #allows ten decimal digits and 26 letters my @letter_from_digit=(0..9,'a'..'z'); -my $base=2; - +my $base=1; RESULT: while($howmany>@results){ ++$base; last RESULT if $base>$largest_base; for my $firstdigit(1..$base-1){ - try([$firstdigit],0,$base, $firstdigit); + my $have=[(0)x$base]; #initialize digits in number + $have->[$firstdigit]++; + try([$firstdigit],0,$base, $firstdigit, $have); last RESULT if @results==$howmany; } } my $n=1; foreach(@results){ say sprintf("%2d- Base %2d: %s", $n++, scalar @$_, - join('', map {$letter_from_digit[$_]} @$_)); + join('', map {$letter_from_digit[$_]} @$_)); } sub try { - my ($current,$position,$base, $sum)=@_; + my ($current,$position,$base, $sum, $had)=@_; if($position==$base-1){ push @results, $current if check($current); return; } - for my $digit(0..$base-$sum){ - try([(@$current,$digit)], $position+1, $base, $sum+$digit); + ++$position; + for my $digit($had->[$position]..$base-$sum){ + my $have=[@$had]; # copy + $have->[$digit]++; + try([(@$current,$digit)], $position, $base, $sum+$digit, $have); } } |
