hermann on Mon, 11 Mar 2024 15:31:03 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: How to generate wolframscript "PowersRepresentations[...]" output in PARI/GP


On 2023-10-02 20:38, hermann@stamm-wilbrandt.de wrote:
https://reference.wolfram.com/language/ref/PowersRepresentations.html
"PowersRepresentations[n,k,p]
    gives the distinct representations of the integer n as
    a sum of k non-negative p^(th) integer powers."

...

(wolframscript is free for Raspberry SBCs)

pi@raspberrypi400:~ $ wolframscript
Wolfram Language 13.3.1 Engine for Linux ARM (64-bit)
Copyright 1988-2023 Wolfram Research, Inc.

In[1]:= PowersRepresentations[17*29,4,2]

Out[1]= {{0, 0, 3, 22}, {0, 0, 13, 18}, {0, 4, 6, 21}, {0, 5, 12, 18},

   {1, 2, 2, 22}, {1, 10, 14, 14}, {2, 2, 14, 17}, {2, 5, 8, 20},

   {2, 8, 8, 19}, {2, 8, 13, 16}, {2, 10, 10, 17}, {3, 4, 12, 18},

   {3, 12, 12, 14}, {4, 4, 10, 19}, {4, 5, 14, 16}, {4, 10, 11, 16},

   {6, 6, 14, 15}, {6, 12, 12, 13}, {8, 8, 13, 14}}

In[2]:=

By the old answer from Bill "foursquares()" is able to give one solution, not all.


I did need this again for a current project, and found PARI/GP "extern()" solution:

pi@raspberrypi5:~ $ gp -q
? n=17*29;
? extern(strjoin(["wolframscript -code \"PowersRepresentations[",Str(n),",4,2]\" | sed \"s/{/\[/g;s/}/\]/g\""])) [[0, 0, 3, 22], [0, 0, 13, 18], [0, 4, 6, 21], [0, 5, 12, 18], [1, 2, 2, 22], [1, 10, 14, 14], [2, 2, 14, 17], [2, 5, 8, 20], [2, 8, 8, 19], [2, 8, 13, 16], [2, 10, 10, 17], [3, 4, 12, 18], [3, 12, 12, 14], [4, 4, 10, 19], [4, 5, 14, 16], [4, 10, 11, 16], [6, 6, 14, 15], [6, 12, 12, 13], [8, 8, 13, 14]]
?


While wolframscript is free on Raspberry computers, a license is needed for other computers. So I implemented "it" from scratch in PARI/GP to have no dependency. With "it" I mean I implemented for p=2 only, and renamed accordingly. It creates unique entries with non-negative non-decreasing values:

hermann@7950x:~$ cat SquaresRepresentations.gp
SquaresRepresentations(n,k,a=0)={
  my(R=List(),m=sqrtint(n\k));
  if(k==1,return(if(m>=a&&n==m^2,[m],[])));
for(b=a,m,foreach(SquaresRepresentations(n-b^2,k-1,b),s,listput(R,concat([b],s))));
  Vec(R);
}

n=eval(getenv("n"));
print(SquaresRepresentations(n,4));
hermann@7950x:~$

hermann@7950x:~$ n=17*29 gp -q < SquaresRepresentations.gp
[[0, 0, 3, 22], [0, 0, 13, 18], [0, 4, 6, 21], [0, 5, 12, 18], [1, 2, 2, 22], [1, 10, 14, 14], [2, 2, 14, 17], [2, 5, 8, 20], [2, 8, 8, 19], [2, 8, 13, 16], [2, 10, 10, 17], [3, 4, 12, 18], [3, 12, 12, 14], [4, 4, 10, 19], [4, 5, 14, 16], [4, 10, 11, 16], [6, 6, 14, 15], [6, 12, 12, 13], [8, 8, 13, 14]]
hermann@7950x:~$


Regards,

Hermann.