XP is just a number PerlMonks

### Re^2: Puzzle Time

by ikegami (Pope)
 on Dec 24, 2012 at 06:41 UTC ( #1010165=note: print w/replies, xml ) Need Help??

in reply to Re: Puzzle Time

Your method of getting the next digit is expensive! Replace
```  for my \$digit (@digits) {
next if \$digit ~~ @number;
push @number, \$digit;
append_digit();
pop @number;
}
with
```  for my \$pos (0..\$#digits) {
push @number, \$digits[\$pos];
local @digits = @digits[0..\$pos-1, \$pos+1..\$#digits];
append_digit();
pop @number;
}

to cut down the time taken to 2/3 (6s from 9s).

Also, there's reason to use a hash (outside of testing).

Full code:

```#!perl
use strict;
use warnings;
use 5.010;

our @digits = 1..9;

my @number;
my @results;
sub append_digit {
my \$number = join "", @number;

push @results, \$number
if @number && !grep \$number % \$_, @number;

for my \$pos (0..\$#digits) {
push @number, \$digits[\$pos];
local @digits = @digits[0..\$pos-1, \$pos+1..\$#digits];
append_digit();
pop @number;
}
}

my \$stime = time;
append_digit();
my \$etime = time;
say \$etime-\$stime;

say "count: " . (0+@results);
#say for sort { \$a <=> \$b } @results;

Create A New User
Node Status?
node history
Node Type: note [id://1010165]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-11-24 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (344 votes). Check out past polls.

Notices?