Well, I have the following code:
Needs["Combinatorica`"];
x = 3051;
threetriples =
KSubsets[If[
Length[Select[
Select[PowersRepresentations[x, 3, 2], Times @@ # != 0 &],
Length[#] == Length[Union[#]] &]] >= 7,
Select[Select[PowersRepresentations[x, 3, 2], Times @@ # != 0 &],
Length[#] == Length[Union[#]] &], Nothing], 3];
testallperms[
three_] := ({n1, n2, n3, n4, n5, n6, n7, n8, n9} = Flatten[three];
Do[{n1, n2, n3, n4, n5, n6, n7, n8, n9} =
NextPermutation[{n1, n2, n3, n4, n5, n6, n7, n8, n9}];
If[x == n1^2 + n2^2 + n3^2 == n4^2 + n5^2 + n6^2 ==
n7^2 + n8^2 + n9^2 == n1^2 + n4^2 + n7^2 ==
n2^2 + n5^2 + n8^2 == n3^2 + n6^2 + n9^2 ==
n1^2 + n5^2 + n9^2, {n1, n2, n3, n4, n5, n6, n7, n8, n9},
Nothing], {9!}]);
DeleteCases[Map[testallperms[#] &, threetriples], Null]
This came from a question I had yesterday.
Now, I have two questions about this:
- The code takes round about 30 minutes to run, is there a way to speed up the calculation?
- The code gave no solution which is not right, because when:
$$n_1=29,n_2=41,n_3=23,n_4=1,n_5=37,n_6=41,n_7=47,n_8=1,n_9=29$$
this must gave a solution to the code. So where is the mistake?