如何解决具有唯一值的每个变量?

时间:2021-10-20 23:07:41

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: 如何解决具有唯一值的每个变量?

使用1到9,只在单元格中使用一次,以便获得行和列产品,如下所示:

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]

如何解决具有唯一值的每个变量?

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

如何修改代码,以便它只返回唯一值为1到9的答案?

Thanks.

1 个解决方案

#1


5  

mypuzzle3[val1_, val2_, val3_, val4_, val5_, val6_] := 
 Select[Permutations[Range @ 9],  With[{p = Partition[#, 3]}, 
  And[Times @@@ p == {val1, val2, val3}, Times @@@ Transpose[p] == {val4, val5, val6}]] &]

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

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

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

#1


5  

mypuzzle3[val1_, val2_, val3_, val4_, val5_, val6_] := 
 Select[Permutations[Range @ 9],  With[{p = Partition[#, 3]}, 
  And[Times @@@ p == {val1, val2, val3}, Times @@@ Transpose[p] == {val4, val5, val6}]] &]

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

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

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