# Solving a discrete equation

Suppose a, b, c, d, e, f, g, h are different digits in the range 1 – 8 which satisfy:

$\qquad \frac{b}{a}+\frac{\text{fgh}}{\text{cde}}=1$

How can I find their value? I tried

Solve[
{b/a + FromDigits[{f, g, h}]/FromDigits[{c, d, e}] == 1,
1 <= {a, b, c, d, e, f, g, h} <= 8,
Unequal @@ {a, b, c, d, e, f, g, h}},
{a, b, c, d, e, f, g, h},
Integers]


but Mathematica cannot get the result from this formulation.

Because there are only

8!


40320

combinations, we can test them all:

perm = Permutations@Range@8;

out = #2/#1 + FromDigits[{#6, #7, #8}]/FromDigits[{#3, #4, #5}] ==  1 & @@@ perm;

pos = Flatten@Position[out, True]


{10534, 10679, 15991, 16333, 16963, 37736, 38041, 39464}

perm[[pos]]


{{3, 1, 6, 7, 8, 4, 5, 2}, {3, 1, 7, 8, 6, 5, 2, 4}, {4, 2, 3, 5, 6, 1, 7, 8}, {4, 2, 7, 1, 6, 3, 5, 8}, {4, 3, 6, 2, 8, 1, 5, 7}, {8, 4, 3, 5, 2, 1, 7, 6}, {8, 4, 7, 1, 2, 3, 5, 6}, {8, 6, 5, 7, 2, 1, 4, 3}}

• Little violent but work well.Thanks.But I think the Solve should can do same thing?
– yode
Nov 10, 2016 at 13:20
• @yode I don't see how there could be a valid Solve[] strategy that can solve this. Nov 10, 2016 at 13:22

I like @corey979 answer, but bunch of #[[..]] doesn't look nice to me. Also you can use Select to avoid many intermediate steps. A one-liner (kinda long one) could be:

Select[Permutations@Range@8, #2/#1 +
FromDigits@{#6, #7, #8}/FromDigits@{#3, #4, #5} & @@ # == 1 &]

• Thanks, I re-made the function in my answer to be less messy. Nov 10, 2016 at 21:06
eqn = b/a + FromDigits[{f, g, h}]/FromDigits[{c, d, e}] == 1;


Since the sum of the terms is 1 and each term is positive, then each term must be less than 1, i.e., a > b && c > f. You can gain some efficiency by prefiltering the list of Permutations with this simpler criteria.

$HistoryLength = 0; ClearSystemCache[]  Without prefiltering (soln = Thread[{a, b, c, d, e, f, g, h} -> #] & /@ Select[Permutations@ Range@8, #2/#1 + FromDigits@{#6, #7, #8}/FromDigits@{#3, #4, #5} & @@ # == 1 &]) // AbsoluteTiming (* {0.201104, {{a -> 3, b -> 1, c -> 6, d -> 7, e -> 8, f -> 4, g -> 5, h -> 2}, {a -> 3, b -> 1, c -> 7, d -> 8, e -> 6, f -> 5, g -> 2, h -> 4}, {a -> 4, b -> 2, c -> 3, d -> 5, e -> 6, f -> 1, g -> 7, h -> 8}, {a -> 4, b -> 2, c -> 7, d -> 1, e -> 6, f -> 3, g -> 5, h -> 8}, {a -> 4, b -> 3, c -> 6, d -> 2, e -> 8, f -> 1, g -> 5, h -> 7}, {a -> 8, b -> 4, c -> 3, d -> 5, e -> 2, f -> 1, g -> 7, h -> 6}, {a -> 8, b -> 4, c -> 7, d -> 1, e -> 2, f -> 3, g -> 5, h -> 6}, {a -> 8, b -> 6, c -> 5, d -> 7, e -> 2, f -> 1, g -> 4, h -> 3}}} *)  Verifying the solutions And @@ (eqn /. soln) (* True *) ClearSystemCache[]  Using prefiltering (soln2 = Thread[{a, b, c, d, e, f, g, h} -> #] & /@ Select[Select[ Permutations@ Range@8, #1 > #2 && #3 > #6 & @@ # &], #2/#1 + FromDigits@{#6, #7, #8}/FromDigits@{#3, #4, #5} & @@ # == 1 &]) // AbsoluteTiming (* {0.138858, {{a -> 3, b -> 1, c -> 6, d -> 7, e -> 8, f -> 4, g -> 5, h -> 2}, {a -> 3, b -> 1, c -> 7, d -> 8, e -> 6, f -> 5, g -> 2, h -> 4}, {a -> 4, b -> 2, c -> 3, d -> 5, e -> 6, f -> 1, g -> 7, h -> 8}, {a -> 4, b -> 2, c -> 7, d -> 1, e -> 6, f -> 3, g -> 5, h -> 8}, {a -> 4, b -> 3, c -> 6, d -> 2, e -> 8, f -> 1, g -> 5, h -> 7}, {a -> 8, b -> 4, c -> 3, d -> 5, e -> 2, f -> 1, g -> 7, h -> 6}, {a -> 8, b -> 4, c -> 7, d -> 1, e -> 2, f -> 3, g -> 5, h -> 6}, {a -> 8, b -> 6, c -> 5, d -> 7, e -> 2, f -> 1, g -> 4, h -> 3}}} *)  The results are identical soln === soln2 (* True *)  Try this: FindInstance[ b/a + Times[f, g, h]/Times[c, d, e] == 1 && 1 < a < 8 && 1 < b < 8 && 1 < f < 8 && 1 < g < 8 && 1 < h < 8 && 1 < c < 8 && 1 < d < 8 && 1 < e < 8, {a, b, c, d, f, g, h, e}, Integers] (* {{a -> 4, b -> 2, c -> 2, d -> 2, f -> 2, g -> 2, h -> 2, e -> 4}} *)  Have fun! • The OP explicitly wrote that$a-h\$ "are from 1 to 8 and different integers"; and FromDigits is crucial here. Nov 10, 2016 at 12:57
• @corey979 Right, I do not claim that this is a final solution. OP will look for it himself. But this is a way to try. Nov 10, 2016 at 13:11
• In my view, this answers a completely different question - it's a different equation. Nov 10, 2016 at 13:13
• fgh is intended as the numeral with those three digits, not their product. Nov 10, 2016 at 16:46
• @Daniel Lichtblau Then it means that I misunderstood the question. Sorry Nov 11, 2016 at 8:38