SEND + MORE = MONEY: Difference between revisions

m
→‎Fast: Add optimizations and timing
m (Remove double header)
m (→‎Fast: Add optimizations and timing)
Line 118:
 
=== Fast ===
Alternately, a version written in 2015 by [http://strangelyconsistent.org/blog/send-more-money-in-perl6 Carl Mäsak]. Not very concise but quite fastspeedy. Applying the observation that M must be 1 and S must be either 8 or 9 gets the runtime under a tenth of a second.
<syntaxhighlight lang="raku" line>my int $s = -17;
while ++$s <= 9 {
next if $s == 0;
Line 140:
my int $send = $s*1000 + $e*100 + $n*10 + $d;
 
my int $m = -1;
while ++$m <= 9 {
next if $m == 0;
next if $m == $s;
next if $m == $e;
next if $m == $n;
next if $m == $d;
 
my int $o = -1;
while ++$o <= 9 {
next if $o == $s;
next if $o == $e;
next if $o == $n;
next if $o == $d;
next if $o == $m;
 
my int $r = -1;
while ++$r <= 9 {
next if $r == $s;
next if $r == $e;
next if $r == $n;
next if $r == $d;
next if $r == $m;
next if $r == $o;
 
my int $more = $m*1000 + $o*100 + $r*10 + $e;
 
my int $y = -1;
while ++$y <= 9 {
next if $y == $s;
next if $y == $e;
next if $y == $n;
next if $y == $d;
next if $y == $m;
next if $y == $o;
next if $y == $r;
 
my int $money =
$m*10000 + $o*1000 + $n*100 + $e*10 + $y;
next unless $send + $more == $money;
say 'SEND + MORE == MONEY';
say "$send + $more == $money";
}
}
}
Line 189 ⟶ 182:
}
}
}
}</syntaxhighlight>
printf "%.3f elapsed seconds", now - INIT now;</syntaxhighlight>
{{out}}
<pre>SEND + MORE == MONEY
9567 + 1085 == 10652</pre>
0.080 elapsed seconds</pre>
 
=={{header|Ring}}==
10,327

edits