This was my take too. Since we start with an even number, and subtract an even number, we don't have to worry about ending in an odd digit. Since we know that all allowed digits will divide any multiple of 504, we don't even test for that.
Here's the code I wrote (somewhat inspired by previous posts):
#!/your/perl/here
use strict;
use warnings;
use Benchmark::Timer;
my $t = Benchmark::Timer->new();
$t->start();
my $c = 7*8*9;
my $x = $c * int(9876321/$c);
my $steps = 0;
while ($x > 0)
{
next if $x =~ /[05]/; # no 0 or 5
next if $x =~ /([1-9]).*\1/; # no repeated digits
last;
}
continue
{
$x -= $c;
$steps++;
}
$t->stop();
print "$x ($steps steps)\n";
print $t->report();
__OUTPUT__
9867312 (17 steps)
1 trial of _default (64us total)
-QM
--
Quantum Mechanics: The dreams stuff is made of
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|