Re: Re:Playing with numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg50239] Re: [mg50223] Re:[mg50135] Playing with numbers
- From: DrBob <drbob at bigfoot.com>
- Date: Sun, 22 Aug 2004 00:19:50 -0400 (EDT)
- References: <200408210704.DAA24819@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
That's pretty cool! It looks at 45 cases, though; I have a solution that looks at 24, with a pre-coding analysis that's no more onerous (I think).
conditions = {h - f == i, f*g == c, e == f*b, a + b == h, c - d == a};
Note g == c/f <= c/2 <= 4. Also g >= 2 as otherwise c == f. The same argument applies to f and b. Hence {b,f,g} is a permutation of {2,3,4}.
Given that, we try solving for the other six variables. But Solve can get only five (there are five equations).
First[Solve[conditions, {a, c, d, e, i}]];
simpleSolve = {a, b, c, d, e, f, g, h, i} /. %
{-b + h, b, f*g, b + f*g - h, b*f, f, g, h, -f + h}
Note that four of these depend on h. The sums h==f+i==a+b imply that h is strictly greater than a, b, i, and f. Since {b, f, g} is a permutation of {2,3,4}, at least one of a and i must be bigger than four. Hence h>=6.
Therefore, the number of possible solutions reduces to:
3!4
24
In particular, the possible values of {b, f, g, h} are:
bfgh = Flatten[Outer[Flatten@{#1, #2} &, Permutations@{2, 3, 4}, Range[6, 9], 1], 1];
Finally we check all 30 cases and list the solutions:
check@{bx_, fx_, gx_, hx_} := Module[
{s = simpleSolve /. Thread[{b, f, g, h} -> {bx, fx, gx, hx}]},
If[Sort@s == Range@9, s, {}]]
check /@ bfgh /. {} -> Sequence[]
{{5, 4, 6, 1, 8, 2, 3, 9, 7}}
That's a lot better than looking at 9!==362880 solutions!!
Bobby
On Sat, 21 Aug 2004 03:04:29 -0400 (EDT), Fred Simons <f.h.simons at tue.nl> wrote:
> The question is to solve with Mathematica the set of equations
> (1) H - F = I
> (2) F x G = C
> (3) E / B = F
> (4) A + B = H
> (5) C - D = A
> where every variable from A to I stands for a number from 1 to 9 and each
> number appears only once.
>
> Here is a solution where Mathematica does the computations that we otherwise
> had to do by hand.
>
> Since the symbols represent different integers between 1 and 9, we conclude
> from the equations i+f=h, a+b=h that h has to be at least 5. From the
> equations c=f*g, e=f*b we conclude that f is between 2 and 4, that g is at
> least 2, that e is at least 2, that c is at least 6 and that e is at least
> 6.
>
> Given values for f, h, c and e, we can compute the other variables. The
> result has to be a permutation of the numbers 1, ..., 9.
>
> In[21]:= solution = {a, b, c, d, e, f, g, h, i} /.
> Solve[{h - f == i, f*g == c, e/b == f,
> a + b == h, c - d == a}, {a, b, d, g, i}][[1]]
>
> Out[21]=
> {-((e - f*h)/f), e/f, c, c + e/f - h, e, f, c/f, h, -f + h}
>
> In[23]:=
> Timing[Do[If[Length[Union[solution]] == 9,
> Print[solution]], {f, 2, 4},
> {c, Ceiling[5/f]*f, 9, f}, {e, Ceiling[5/f]*f, 9, f}, {h, 5, 9}]]
>
>> From In[23]:=
> {5,4,6,1,8,2,3,9,7}
>
> Out[23]=
> {0. Second,Null}
>
> So indeed there is only one solution.
>
> Fred Simons
> Eindhoven University of Technology
>
>
>
--
DrBob at bigfoot.com
www.eclecticdreams.net
- References:
- Re:Playing with numbers
- From: "Fred Simons" <f.h.simons@tue.nl>
- Re:Playing with numbers