SEND + MORE = MONEY: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(SEND + MORE = MONEY in Gambas)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(13 intermediate revisions by 9 users not shown)
Line 247:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
{{trans|Julia}}
<syntaxhighlight lang="c">#include <stdio.h>
 
int main() {
int m = 1, s, e, n, d, o, r, y, sum1, sum2;
const char *f = "%d%d%d%d + %d%d%d%d = %d%d%d%d%d\n";
for (s = 8; s < 10; ++s) {
for (e = 0; e < 10; ++e) {
if (e == m || e == s) continue;
for (n = 0; n < 10; ++n) {
if (n == m || n == s || n == e) continue;
for (d = 0; d < 10; ++d) {
if (d == m || d == s || d == e || d == n) continue;
for (o = 0; o < 10; ++o) {
if (o == m || o == s || o == e || o == n || o == d) continue;
for (r = 0; r < 10; ++r) {
if (r == m || r == s || r == e || r == n || r == d || r == o) continue;
for (y = 0; y < 10; ++y) {
if (y == m || y == s || y == e || y == n || y == d || y == o) continue;
sum1 = 1000*s + 100*e + 10*n + d + 1000*m + 100*o + 10*r + e;
sum2 = 10000*m + 1000*o + 100*n + 10*e + y;
if (sum1 == sum2) {
printf(f, s, e, n, d, m, o, r, e, m, o, n, e, y);
}
}
}
}
}
}
}
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
9567 + 1085 = 10652
</pre>
 
=={{header|FreeBASIC}}==
Line 366 ⟶ 406:
 
Took 1.149804ms.
</pre>
 
=={{header|J}}==
'''Tacit Solution'''
<syntaxhighlight lang="j">SEND=. 10 #. 0 1 2 3&{
MORE=. 10 #. 4 5 6 1&{
MONEY=. 10 #. 4 5 2 1 7&{
M=. 4&{
entry=. 0&{::
try=. 1&{::
sample=. (10 ?~ 8:) ; 1 + try NB. counting tries to avoid a premature convergence
good=. (not=. -.) (o=.@:) (0 = M) (and=. *.) (SEND + MORE) = MONEY
answer=. (": o SEND , ' + ' , ": o MORE , ' = ' , ": o MONEY) o entry
tries=. ', random tries ' , ": o try
while=. ^: (^:_)
solve=. (answer , tries) o (sample while (not o good o entry)) o ( 0 ;~ i.) o 8: f.</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j"> solve ''
9567 + 1085 = 10652, random tries 248241
solve ''
9567 + 1085 = 10652, random tries 246504
solve ''
9567 + 1085 = 10652, random tries 3291556</syntaxhighlight>
 
The code is tacit and fixed (in other words, it is point-free):
<syntaxhighlight lang="j"> _80 [\ (5!:5)<'solve'
((":@:(10 (#.) 0 1 2 3&({ )) , ' + ' , ":@:(10 (#.) 4 5 6 1&({ )) , ' = ' , ":@:
(10 (#.) 4 5 2 1 7&({ )))@:(0&({::)) , ', random tries ' , ":@:(1&({::)))@:(((10
?~ 8:) ; 1 + 1&({::))^:(-.@:(-.@:(0 = 4&({ )) *. ((10 (#.) 0 1 2 3&({ )) + 10 (
#.) 4 5 6 1&({ )) = 10 (#.) 4 5 2 1 7&({ ))@:(0&({::)))^:_)@:(0 ;~ i.)@:8:</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq, and with fq'''
 
Straight out of the [https://en.wikipedia.org/wiki/Jq_(programming_language) wikipedia] page, except for {} instead of [] in the last line of the def:
<syntaxhighlight lang=jq>
def send_more_money:
def choose(m;n;used): ([range(m;n+1)] - used)[];
def num(a;b;c;d): 1000*a + 100*b + 10*c + d;
def num(a;b;c;d;e): 10*num(a;b;c;d) + e;
first(
1 as $m
| 0 as $o
| choose(8;9;[]) as $s
| choose(2;9;[$s]) as $e
| choose(2;9;[$s,$e]) as $n
| choose(2;9;[$s,$e,$n]) as $d
| choose(2;9;[$s,$e,$n,$d]) as $r
| choose(2;9;[$s,$e,$n,$d,$r]) as $y
| select(num($s;$e;$n;$d) + num($m;$o;$r;$e) == num($m;$o;$n;$e;$y))
| {$s,$e,$n,$d,$m,$o,$r,$e,$m,$o,$n,$e,$y} );
 
send_more_money
</syntaxhighlight>
{{output}}
<pre>
{"s":9,"e":5,"n":6,"d":7,"m":1,"o":0,"r":8,"y":2}
</pre>
 
Line 403 ⟶ 510:
end
</syntaxhighlight>{{out}} 9567 + 1085 == 10652
 
=={{header|Nim}}==
{{trans|Julia}}
<syntaxhighlight lang="Nim">import std/strformat
 
let m = 1
for s in 8..9:
for e in 0..9:
if e in [m, s]: continue
for n in 0..9:
if n in [m, s, e]: continue
for d in 0..9:
if d in [m, s, e, n]: continue
for o in 0..9:
if o in [m, s, e, n, d]: continue
for r in 0..9:
if r in [m, s, e, n, d, o]: continue
for y in 0..9:
if y in [m, s, e, n, d, o]: continue
if 1000 * s + 100 * e + 10 * n + d + 1000 * m + 100 * o + 10 * r + e ==
10000 * m + 1000 * o + 100 * n + 10 * e + y:
echo &"{s}{e}{n}{d} + {m}{o}{r}{e} = {m}{o}{n}{e}{y}"
</syntaxhighlight>
 
{{out}}
<pre>9567 + 1085 = 10652
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Line 658 ⟶ 793:
O=1 Y=4 E=0 N=6 M=2 T=9 S=3 R=8 A=7 H=5
496179 checks 00:00.013 secs</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
=== Exhaustive ===
<syntaxhighlight lang="perl" line>use v5.36;
use enum <D E M N O R S Y>;
use Algorithm::Combinatorics <combinations permutations>;
 
sub solve {
for my $p (map { permutations $_ } combinations [0..9], 8) {
return $p if @$p[M] > 0 and join('',@$p[S,E,N,D])+join('',@$p[M,O,R,E]) == join('',@$p[M,O,N,E,Y]);
}
}
 
printf "SEND + MORE == MONEY\n%d + %d == %d", join('',@$_[S,E,N,D]), join('',@$_[M,O,R,E]), join '',@$_[M,O,N,E,Y]) for solve();</syntaxhighlight>
{{out}}
<pre>SEND + MORE == MONEY
9567 + 1085 == 10652</pre>
=== Fine-tuned ===
<syntaxhighlight lang="perl" line>use v5.36;
 
my $s = 7;
while (++$s <= 9) {
my $e = -1;
while (++$e <= 9) {
next if $e == $s;
my $n = -1;
while (++$n <= 9) {
next if grep { $n == $_ } $s,$e;
my $d = -1;
while (++$d <= 9) {
next if grep { $d == $_ } $s,$e,$n;
my $send = $s*10**3 + $e*10**2 + $n*10 + $d;
my ($m, $o) = (1, -1);
while (++$o <= 9) {
next if grep { $o == $_ } $s,$e,$n,$d,$m;
my $r = -1;
while (++$r <= 9) {
next if grep { $r == $_ } $s,$e,$n,$d,$m,$o;
my $more = $m*10**3 + $o*10**2 + $r*10 + $e;
my $y = -1;
while (++$y <= 9) {
next if grep { $y == $_ } $s,$e,$n,$d,$m,$o,$r;
my $money = $m*10**4 + $o*10**3 + $n*10**2 + $e*10 + $y;
next unless $send + $more == $money;
say "SEND + MORE == MONEY\n$send + $more == $money";
}
}
}
}
}
}
}
 
</syntaxhighlight>
{{out}}
<pre>SEND + MORE == MONEY
9567 + 1085 == 10652</pre>
 
=={{header|Phix}}==
Line 671 ⟶ 864:
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">avail</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ad</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ct</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">correct_to</span><span style="color: #0000FF;">[</span><span style="color: #000000;">done</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">carry</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bOK</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">iffor</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ct</span> <span style="color: #008080;">thendo</span>
<span style="color: #004080000000;">integercarry</span> <span style="color: #0000000000FF;">carry+=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0answer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bOKsums</span> <span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #004600000000;">1</span><span style="color: #0000FF;">true]))</span>
<span style="color: #008080;">forif</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">icarry</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1answer</span><span style="color: #0000FF;">[</span><span style="color: #008080000000;">tosums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">cti</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]]</span> <span style="color: #008080;">dothen</span>
<span style="color: #000000;">carrybOK</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8004600;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))false</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">carry</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]]</span> <span style="color: #008080;">then</span>
<span style="color: #000000008080;">bOK</span> <span style="color: #0000FF;">=end</span> <span style="color: #004600008080;">falseif</span>
<span style="color: #000000;">carry</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">carry</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">iffor</span>
<span style="color: #000000008080;">carryif</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">carrybOK</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF008080;">)then</span>
<span style="color: #008080;">endif</span> <span style="color: #000000;">ct</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">forthen</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bOKcarry</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">answer</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ct</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #008080000000;">ifsums</span><span style="color: #0000FF;">,</span><span style="color: #000000;">carrysolve_order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">firsts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0correct_to</span><span style="color: #0000FF;">,</span><span style="color: #008080000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">avail</span><span style="color: #0000FF;">-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">done</span><span style="color: #0000FF;">then)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">answer</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve_order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">firsts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">correct_to</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">avail</span><span style="color: #0000FF;">-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">done</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve_order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">firsts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">correct_to</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">avail</span><span style="color: #0000FF;">-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">done</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
Line 803 ⟶ 989:
+ 1085
= 10652
</pre>
 
=={{header|Python}}==
{{trans|Nim}}
<syntaxhighlight lang="python3">
# SEND + MORE = MONEY by xing216
m = 1
for s in range(8,10):
for e in range(10):
if e in [m, s]: continue
for n in range(10):
if n in [m, s, e]: continue
for d in range(10):
if d in [m, s, e, n]: continue
for o in range(10):
if o in [m, s, e, n, d]: continue
for r in range(10):
if r in [m, s, e, n, d, o]: continue
for y in range(10):
if y in [m, s, e, n, d, o]: continue
if 1000 * s + 100 * e + 10 * n + d + 1000 * m + 100 * o + 10 * r + e == \
10000 * m + 1000 * o + 100 * n + 10 * e + y:
print(f"{s}{e}{n}{d} + {m}{o}{r}{e} = {m}{o}{n}{e}{y}")
</syntaxhighlight>
{{out}}
<pre>
9567 + 1085 = 10652
</pre>
 
Line 836 ⟶ 1,049:
=== 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 $n = -1;
while ++$n ≤ 9 {
next if $n == $s|$e;
my $d = -1;
while ++$d ≤ 9 {
next if $d == $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 $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";
}
}
Line 987 ⟶ 1,174:
<syntaxhighlight lang="ring">
// Author: Gal Zsolt 2023-02-08
t1 = clock() // start
see "works..." + nl + nl
aListSend = []
Line 1,048 ⟶ 1,234:
next
next
see "Time: "+ clock() - t1 // end
see "done..." + nl
</syntaxhighlight>
Line 1,056 ⟶ 1,241:
SEND = 9567 MORE = 1085 MONEY = 10652
done...
</pre>
Time: 31.9 s
 
=={{header|Ruby}}==
Solving for the string "SEND + 1ORE == 1ONEY" using 'tr' , which translates characters to other characters. The resulting string is brutally evalled.
<syntaxhighlight lang="ruby">str = "SEND + 1ORE == 1ONEY"
digits = [0,2,3,4,5,6,7,8,9] # 1 is absent
uniq_chars = str.delete("^A-Z").chars.uniq.join
res = digits.permutation(uniq_chars.size).detect do |perm|
num_str = str.tr(uniq_chars, perm.join)
next if num_str.match?(/\b0/) #no words can start with 0
eval num_str
end
puts str.tr(uniq_chars, res.join)
</syntaxhighlight>
{{out}}
<pre>9567 + 1085 == 10652
</pre>
 
=={{header|Vala}}==
{{trans|C}}
<syntaxhighlight lang="vala">void main() {
int m = 1, s, e, n, d, o, r, y, sum1, sum2;
string f = "%d%d%d%d + %d%d%d%d = %d%d%d%d%d\n";
for (s = 8; s < 10; ++s) {
for (e = 0; e < 10; ++e) {
if (e == m || e == s) continue;
for (n = 0; n < 10; ++n) {
if (n == m || n == s || n == e) continue;
for (d = 0; d < 10; ++d) {
if (d == m || d == s || d == e || d == n) continue;
for (o = 0; o < 10; ++o) {
if (o == m || o == s || o == e || o == n || o == d) continue;
for (r = 0; r < 10; ++r) {
if (r == m || r == s || r == e || r == n || r == d || r == o) continue;
for (y = 0; y < 10; ++y) {
if (y == m || y == s || y == e || y == n || y == d || y == o) continue;
sum1 = 1000*s + 100*e + 10*n + d + 1000*m + 100*o + 10*r + e;
sum2 = 10000*m + 1000*o + 100*n + 10*e + y;
if (sum1 == sum2) {
print(f, s, e, n, d, m, o, r, e, m, o, n, e, y);
}
}
}
}
}
}
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
9567 + 1085 = 10652
</pre>
 
=={{header|Wren}}==
Clearly M = 1 and S must be 8 or 9. Brute force can be used to solve for the other letters.
<syntaxhighlight lang="ecmascriptwren">var start = System.clock
var sends = []
var ors = []
9,476

edits