Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
in thread 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; }
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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010165]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2017-07-26 11:17 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (390 votes). Check out past polls.