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..$pos1, $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..$pos1, $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;
