SEND + MORE = MONEY: Difference between revisions

→‎Raku:Fast: less verbose with junctions, but no loss of speed ('int' wasn't helping)
(Added Perl)
(→‎Raku:Fast: less verbose with junctions, but no loss of speed ('int' wasn't helping))
Line 901:
=== 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 speedy. 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 = 7;
while ++$s <= 9 {
next ifmy $se == 0-1;
while ++$e <= 9 {
 
my int $e = -1;
while ++$e <= 9 {
next if $e == $s;
my int my $en = -1;
while ++$on <= 9 {
next if $dn == $s|$e;
my int $od = -1;
while ++$rd <= 9 {
next if $od == $s|$e|$n;
 
my int my $nsend = -1$s×10³ + $e×10² + $n×10 + $d;
while ++ my ($m, $no) <= 91, {-1;
next if $n == while ++$s;o ≤ 9 {
next if $no == $s|$e|$n|$d|$m;
 
my int my $dr = -1;
while ++$dr <= 9 {
next if $dr == $s|$e|$n|$d|$m|$o;
next if $d == $e;
next if my $dmore == $nm×10³ + $o×10² + $r×10 + $e;
my int $y = -1;
 
my int $send = $s*1000 + $e*100 + $n*10while ++$y $d;≤ 9 {
next if $y == $s|$e|$n|$d|$m|$o|$r;
 
my int $m = 1;
 
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×10⁴ + $o×10³ + $n×10² + $e×10 + $y;
$m*10000 + $o*1000 + $n*100 + $e*10 + $y;
next unless $send + $more == $money;
say 'SEND + MORE == MONEY' ~ "\n$send + $more == $money";
say "$send + $more == $money";
}
}
2,392

edits