American Citizen on Fri, 17 Nov 2023 22:54:10 +0100


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

question on efficient but exhaustive 3 squares algorithm


Recent emails shared in the group has caught my attention in regard to finding an efficient algorithm for solutions to a certain ternary quadratic form, specifically, ternary forms of the type [1,1,1,0,0,0] = N which are solutions to N = x^2 + y^2 + z^2 or 3 squares which sum to N

As is known, 7, 15  and 23 are the first three numbers which cannot be represented by the sum of 3 squares.

I took the algorithm from the mail list:

  threesquare(n) = abs(qfsolve(matdiagonal([1,1,1,-n]))[1..3]);

and ran 1 <= n <= 25 (except 7 and 15 and 23 of course)

I compared that output with that from my sqs3(N) algorithm: (sqs2() is needed by that algorithm)

{sqs2(N)=
local(L,i,R);
L=abs(qfbsolve(Qfb(1,0,1),N,3));
for(i=1,matsize(L)[2],L[i]=vecsort(L[i],,4));
R=Set(L);
return(R);
}
{sqs3(N)=
local(R,L,t,i,M,K);
R=Set();
if(N==0,return([[0,0,0]]));
for(t=0,sqrtint(N),
  M=N-t*t;
  K=sqs2(M);
  L=Set(vector(matsize(K)[2],i,vecsort([t,K[i][1],K[i][2]],,4)));
  \\ if(L!=[], for(i=1,matsize(L)[2],print(L[i])));
  R=setunion(R,L);
);
return(R);
};

And I found, comparing the two algorithms that specifically:

  for n = [1..8] (skipping n=7) the results are identical

  for n = 9, threesquare only posts [0,0,3], but actually there is [2,2,1] also

  for n = [10..14] results are identical

  skip n = 15

  n = 16, both identical

  n = 17, threesquare only gives [4,1,0], but [3,2,2] also exists
  n = 18, threesquare only gives [0,3,3], but [4,1,1] also exists

  for n = [19..24] results are identical (skipping n = 23)

  n = 25, threesquare only gives [0,0,5] but [4,3,0] also exists

It can be seen that threesquare is missing some solutions. Furthermore threesquares() blows up for N=7,15,23, etc.

Can we derive an exhaustive threesquares(N) algorithm which does give all solutions for a specific N? and returns [] if no such representation occurs. This is the same as solving the [1,1,1,0,0,0] ternary quadratric form.

- Randall

P.S.: My imagination is intrigued by a new number type in GP-Pari, called "Qft" for Quadratic ternary form and it takes a vector [a,b,c,d,e,f] or 6 elements for composition. Just a thought ??