5
$\begingroup$

I have to solve this equation, where each variable is an integer from 1-9, but every variable has a different value:

(a/(10 b+c))+(d/(10 e+f))+(g/(10 h+i)) == 1
$\endgroup$
6
  • $\begingroup$ Brute force should suffice for this. $\endgroup$ Commented Jan 8, 2017 at 21:40
  • $\begingroup$ @DanielLichtblau That's $9^9$ combinations. $\endgroup$
    – Feyre
    Commented Jan 8, 2017 at 21:50
  • 2
    $\begingroup$ @Feyre No, only $9!$ since all are pairwise unequal. $\endgroup$ Commented Jan 8, 2017 at 21:52
  • $\begingroup$ @DanielLichtblau So much for my reading comprehension. $\endgroup$
    – Feyre
    Commented Jan 8, 2017 at 22:04
  • 1
    $\begingroup$ @Corey979 Not really related though your approach is in each case (and I upvoted both). The salient difference is that, as best I can tell, today's question cannot be cast as an integer linear program (ILP, emphasis on the L). So it is not clear that there is any alternative to brute force. Well, not clear to me at least. $\endgroup$ Commented Jan 8, 2017 at 22:28

2 Answers 2

7
$\begingroup$

There are 9! == 362880 combinations (because every variable has a different value) so we can check all of them.

p = Permutations @ Range @ 9;

pos = Flatten @
  Position[(#1/(10 #2 + #3) + #4/(10 #5 + #6) + #7/(10 #8 + #9) == 1) & @@@ p, True]

{173225, 173403, 271265, 271325, 322803, 323135}

p[[pos]]

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

Hence, there are six solutions as listed above.


More compactly (but requiring about 20% more time):

s = Select[Permutations @ Range @ 9,
       #1/(10 #2 + #3) + #4/(10 #5 + #6) + #7/(10 #8 + #9) & @@ # == 1 &]

p[[pos]] == s

True

ListLinePlot @ s

enter image description here

$\endgroup$
5
$\begingroup$
With[{perm = Permutations@Range@9}, 
 Pick[perm, (#1/(10. #2 + #3) + #4/(10. #5 + #6) + #7/(10. #8 + #9) - 1) &
  @@ Transpose@perm // Unitize, 0]]
$\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.