SEND + MORE = MONEY: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(10 intermediate revisions by 7 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 367 ⟶ 407:
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}}==
Line 432 ⟶ 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}}==
Line 688 ⟶ 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 826 ⟶ 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 859 ⟶ 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 1,010 ⟶ 1,174:
<syntaxhighlight lang="ring">
// Author: Gal Zsolt 2023-02-08
t1 = clock() // start
see "works..." + nl + nl
aListSend = []
Line 1,071 ⟶ 1,234:
next
next
see "Time: "+ clock() - t1 // end
see "done..." + nl
</syntaxhighlight>
Line 1,079 ⟶ 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