10
$\begingroup$

I have a puzzle for example:

Use 1 to 9, once only in the cells so that you obtain row and column products as shown: enter image description here

mypuzzle2[val1_, val2_, val3_, val4_, val5_, val6_] :=  Solve[{a*b*c == val1, d*e*f == val2, g*h*i == val3, a*d*g == val4,    b*e*h == val5, c*f*i == val6, a > 0, b > 0, c > 0, d > 0, e > 0,    f > 0, g > 0, h > 0, i > 0, a < 10, b < 10, c < 10, d < 10, e < 10,     f < 10, g < 10, h < 10, i < 10}, {a, b, c, d, e, f, g, h, i},   Integers]


mypuzzle2[80, 63, 72, 72, 48, 105]

enter image description here

How do I modify the code, so that it only returns with the answer with unique values of 1 to 9?

Thanks.

$\endgroup$

1 Answer 1

7
$\begingroup$
mypuzzle3[val__] /; Length[{val}] == 6 := Select[Permutations[Range @ 9], 
 With[{p = Partition[#, 3]}, Times @@@ Join[p, Transpose @ p] == {val}] &]

mypuzzle3[80, 63, 72, 72, 48, 105]

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

Also:

Select[mypuzzle2[80, 63, 72, 72, 48, 105], Union[#[[All,-1]]]==Range[9]&]  (* or *)
Select[mypuzzle2[80, 63, 72, 72, 48, 105], DuplicateFreeQ[#[[All, -1]]] &]

{{a -> 2, b -> 8, c -> 5, d -> 9, e -> 1, f -> 7, g -> 4, h -> 6,   i -> 3}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.