SEND + MORE = MONEY: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (Added Perl) |
SqrtNegInf (talk | contribs) (→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 |
<syntaxhighlight lang="raku" line>my $s = 7; |
||
while ++$s |
while ++$s ≤ 9 { |
||
my $e = -1; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
next if $e == $s; |
next if $e == $s; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
my $send = $s×10³ + $e×10² + $n×10 + $d; |
|||
my ($m, $o) = 1, -1; |
|||
while ++$o ≤ 9 { |
|||
next if $ |
next if $o == $s|$e|$n|$d|$m; |
||
my $r = -1; |
|||
while ++$ |
while ++$r ≤ 9 { |
||
next if $ |
next if $r == $s|$e|$n|$d|$m|$o; |
||
⚫ | |||
my $more = $m×10³ + $o×10² + $r×10 + $e; |
|||
⚫ | |||
while ++$y ≤ 9 { |
|||
⚫ | |||
my int $m = 1; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
next if $o == $e; |
|||
next if $o == $n; |
|||
next if $o == $d; |
|||
next if $o == $m; |
|||
my int $r = -1; |
|||
⚫ | |||
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; |
|||
⚫ | |||
while ++$y <= 9 { |
|||
⚫ | |||
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 |
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"; |
|||
} |
} |
||
} |
} |