SEND + MORE = MONEY: Difference between revisions

Content added Content deleted
(Added Perl)
(→‎Raku:Fast: less verbose with junctions, but no loss of speed ('int' wasn't helping))
Line 901: Line 901:
=== Fast ===
=== 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.
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;
<syntaxhighlight lang="raku" line>my $s = 7;
while ++$s <= 9 {
while ++$s 9 {
next if $s == 0;
my $e = -1;
while ++$e 9 {

my int $e = -1;
while ++$e <= 9 {
next if $e == $s;
next if $e == $s;
my $n = -1;
while ++$n 9 {
next if $n == $s|$e;
my $d = -1;
while ++$d 9 {
next if $d == $s|$e|$n;


my int $n = -1;
my $send = $s×10³ + $e×10² + $n×10 + $d;
while ++$n <= 9 {
my ($m, $o) = 1, -1;
next if $n == $s;
while ++$o ≤ 9 {
next if $n == $e;
next if $o == $s|$e|$n|$d|$m;

my int $d = -1;
my $r = -1;
while ++$d <= 9 {
while ++$r 9 {
next if $d == $s;
next if $r == $s|$e|$n|$d|$m|$o;
next if $d == $e;
next if $d == $n;
my $more = $m×10³ + $o×10² + $r×10 + $e;
my $y = -1;

my int $send = $s*1000 + $e*100 + $n*10 + $d;
while ++$y ≤ 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 =
my $money = $m×10⁴ + $o×10³ + $n×10² + $e×10 + $y;
$m*10000 + $o*1000 + $n*100 + $e*10 + $y;
next unless $send + $more == $money;
next unless $send + $more == $money;
say 'SEND + MORE == MONEY';
say 'SEND + MORE == MONEY' ~ "\n$send + $more == $money";
say "$send + $more == $money";
}
}
}
}