SEND + MORE = MONEY: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added C)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(3 intermediate revisions by 3 users not shown)
Line 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 951 ⟶ 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 1,192 ⟶ 1,257:
{{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