0
$\begingroup$
Needs["Combinatorica`"]
five[n_] := 
 Module[{v = 
    SparseArray[LowerTriangularize[Outer[Plus, #, #] &[Range[n]^4]]][
     "NonzeroValues"], t, k, x55, a, b, s, x1, x2, x3, x4}, 
  k = Ordering@v;
  t = v[[k]];
  Monitor[Do[x55 = x5^4; a = 1;
    Do[s = x55 - t[[b]]; While[t[[a]] < s, ++a];
     If[t[[a]] == s, x2 = Round[Sqrt[2 k[[a]]]];
      x1 = k[[a]] - x2 (x2 - 1)/2; x4 = Round[Sqrt[2 k[[b]]]];
      x3 = k[[b]] - x4 (x4 - 1)/2;
      If[x2 <= x3 || x4 <= x1, Print@{x1, x2, x3, x4, x5}]], {b, 
      Ceiling@BinarySearch[t, x55/2], 1, -1}], {x5, 2, n}], 
   Row[{ProgressIndicator[x5, {2, n}], x5}, " "]]
  ]
(*in:five[353]
out:res = {272, 315, 30, 120, 353}
res[[1]]^4 + res[[2]]^4 + res[[3]]^4 + res[[4]]^4 - res[[5]]^4==0*)

The C language environment is already configured. I have tried the following code:

Needs["Combinatorica`"]
Needs["CCompilerDriver`GenericCCompiler`"]
$CCompiler = {"Compiler" -> 
    CCompilerDriver`GenericCCompiler`GenericCCompiler, 
   "CompilerInstallation" -> "D:\\Program Files (x86)\\mingw64\\bin", 
   "CompilerName" -> "gcc.exe"};
five = Compile[{{n, _Integer}},
  Module[{v = 
     SparseArray[LowerTriangularize[Outer[Plus, #, #] &[Range[n]^4]]][
      "NonzeroValues"], t, k, x55, a, b, s, x1, x2, x3, x4}, 
   k = Ordering@v;
   t = v[[k]];
   Monitor[Do[x55 = x5^4; a = 1;
     Do[s = x55 - t[[b]]; While[t[[a]] < s, ++a];
      If[t[[a]] == s, x2 = Round[Sqrt[2 k[[a]]]];
       x1 = k[[a]] - x2 (x2 - 1)/2; x4 = Round[Sqrt[2 k[[b]]]];
       x3 = k[[b]] - x4 (x4 - 1)/2;
       If[x2 <= x3 || x4 <= x1, Print@{x1, x2, x3, x4, x5}]], {b, 
       Ceiling@BinarySearch[t, x55/2], 1, -1}], {x5, 2, n}], 
    Row[{ProgressIndicator[x5, {2, n}], x5}, " "]]
   ],
  RuntimeAttributes -> {Listable}, Parallelization -> True, 
  CompilationTarget -> "C"]

warning It seems to have failed, and there has been no significant improvement in speed.

$\endgroup$

1 Answer 1

1
$\begingroup$

The following code is original by @flc:

Needs["CCompilerDriver`GenericCCompiler`"]
$CCompiler = {"Compiler" -> 
    CCompilerDriver`GenericCCompiler`GenericCCompiler, 
   "CompilerInstallation" -> "D:\\Program Files (x86)\\mingw64\\bin", 
   "CompilerName" -> "gcc.exe"};
cf = With[{g = Compile`GetElement}, 
   Compile[{{v, _Integer, 1}, {k, _Integer, 1}, {x5, _Integer}}, 
    Module[{x55 = x5^4, a = 1, l = 1, r = Length@v + 1, m, t, x, x1, 
      x2, x3, x4}, x = BitShiftRight[x55, 1] + 1;
     While[l + 1 < r, m = BitShiftRight[l + r, 1];
      If[g[v, m] < x, l = m, r = m]];
     Do[t = x55 - g[v, b]; While[g[v, a] < t, ++a];
      If[g[v, a] == 
        t, {x2, x4} = {Round[Sqrt[2 g[k, a]]], 
         Round[Sqrt[2 g[k, b]]]}; {x1, 
         x3} = {g[k, a] - BitShiftRight[x2 (x2 - 1), 1], 
         g[k, b] - BitShiftRight[x4 (x4 - 1), 1]};
       If[x2 <= x3 || x4 <= x1, Print@{x1, x2, x3, x4, x5}]], {b, l, 
       1, -1}]], CompilationTarget -> "C", RuntimeOptions -> "Speed"]];
five[n_] := 
 Module[{v = 
    SparseArray[LowerTriangularize[Outer[Plus, #, #] &[Range[n]^4]]][
     "NonzeroValues"], k}, k = Ordering@v; v = v[[k]];
  Monitor[Do[cf[v, k, x5], {x5, 2, n}], 
   Row[{ProgressIndicator[x5, {2, n}], x5}, " "]]]

It is indeed much faster than the uncompiled version.

$\endgroup$
1
  • $\begingroup$ Another Writing Method for Assigning Values to v : v = Reverse@GroupTheory`Tools`Multisets[Range[n, 1, -1], 2]^4 . {1, 1} $\endgroup$
    – miss
    Commented Oct 26, 2023 at 10:56

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.