Well, I have the following problem: I need to solve the following system of equations:

$$ \begin{cases} n=a^3+b^3+c^3\\ \\ n=d^3+k^3+f^3 \end{cases}\tag1 $$


  • $n\ne a\ne b\ne c\ne d\ne k\ne f$;
  • $1\le a<n,1\le b<n,1\le c<n,1\le d<n,1\le k<n,1\le f<n$;
  • $n\in\mathbb{N}^+,a\in\mathbb{N}^+,b\in\mathbb{N}^+,c\in\mathbb{N}^+,d\in\mathbb{N}^+,k\in\mathbb{N}^+,f\in\mathbb{N}^+$

And I want to search for solutions using the following range: $2\le n\le1000^3$.

I thought of the following code:


Question: is there a faster way to let Mathematica evaluate this?


After seeing the answer that was posted by @kglr, I wondered can I use that technique to solve the same problem by using more variables:

$$ \begin{cases} n=a^3+b^3+c^3\\ \\ n=d^3+k^3+f^3\\ \\ n=g^3+h^3+m^3\\ \\ n=a^3+k^3+m^3\\ \\ n=g^3+k^3+c^3\\ \\ n=a^3+d^3+g^3\\ \\ n=b^3+k^3+h^3\\ \\ n=c^3+f^3+m^3 \end{cases}\tag2 $$

All the conditions are the same, so they all can not be equal to each other and needs to be an integer bigger than zero.


1 Answer 1

While[Length[pr] < 2, 
  pr = Select[FreeQ @ 0] @ PowersRepresentations[RandomInteger[{2, 10^6}], 3, 3]]; //
     AbsoluteTiming // First
 {{a, b, c}, {d, k, f}, n} = Join[pr[[;;2]], {Total[pr[[1]]^3]}]
{{2, 3, 16}, {3, 9, 15}, 4131}
Total[pr^3, {2}]
{4131, 4131}

If you want the 6 numbers all distinct:

While[Length[Union @@ pr] < 6, 
    pr = Select[FreeQ@0]@
      PowersRepresentations[RandomInteger[{2, 10^6}], 3, 3]]; // 
  AbsoluteTiming // First
 {{5, 53, 88}, {12, 29, 93}, {15, 74, 75}}
  • $\begingroup$ @Jan, hope the new version makes it easier to see the steps: We pick a random integer in the interval 2 to 20^6 (RandomInteger[{2, 10^6}]), find its representations as the sum of 3 cubes (PowersRepresentations[..., 3,3]), select the triples free of 0 (Select[FreeQ @0]) , we repeat as long as the length of triples we get is less than 2 (Lenth[pr]<2) . $\endgroup$
    – kglr
    Commented Apr 16, 2020 at 13:56
  • $\begingroup$ First of all, I am very thankful for your time and answer, so +1. I understand your code. Can you help me with the problem I edited into my question, using your technique? Because that is the full problem I would like to solve. $\endgroup$ Commented Apr 16, 2020 at 14:07
  • $\begingroup$ @Jan, it is unlikely that we can find 5 sets of distinct triples in Range[2, 10^6] , that is, we probably end up with an infinite while loop if we use random search without eliminating previously seen points. $\endgroup$
    – kglr
    Commented Apr 16, 2020 at 14:52
  • $\begingroup$ some variables are used twice, so it is not really 5 sets of distinct triples. For example the variable $a$ turns up in the first and 4th equation. So maybe extend the range or something. Can you help me with that? $\endgroup$ Commented Apr 16, 2020 at 14:56
  • $\begingroup$ I also made a mistake, it should be 8 equations which are interlinked with each other. $\endgroup$ Commented Apr 16, 2020 at 14:58

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.