Re: expressing a number as the sum of 3 triangular numbers

Max Alekseyev <[email protected]> Sat, 15 Nov 2025 21:28:35 -0500
Newsgroups gmane.comp.mathematics.pari.user
Message-ID <CAJkPp5PYQxXGQFpRdqKdAn1nHAnwJ5wKqxfR_yPCg_LQoC3ouw@mail.gmail.com>
You can replace sqs3 with

{ sqs3_(N) = my(s = qfsolve(matdiagonal([1,1,1,-N])));
vector(3,i,abs(s[i]/s[4])); }

Regards,
Max


On Sat, Nov 15, 2025 at 7:19 PM American Citizen <[email protected]>
wrote:

> This is my attempt to fulfill Bill Allombert's request for an exercise
> to express a number n as the sum of 3 triangular numbers.
>
> tri(n)=n*(n+1)/2;
>
> {sqs3(N) =
> my(a);
> if(N==0,return([0,0,0]););
> a=Set(concat(vector(sqrtint(N\3+1)+1,j,
>      my(i=j-1);
> Set(abs([vecsort(concat(i,abs(v)),,4)|v<-qfbsolve(Qfb(1,0,1),N-i^2,3)]));
>    )));
> if(a==[],return(a); , return(a[1]); );
> }
>
> {three_tri(n)=
> my(N,K,a,b,c,M);
> N=8*n+3;
> print("N=",N);
> K=sqs3(N);
> print("K=",K);
> if(K!=[], [a,b,c]=[(K[1]-1)/2, (K[2]-1)/2, (K[3]-1)/2]; ,
> [a,b,c]=[0,0,0]; );
> print("a,b,c = ",[a,b,c]);
> return([a,b,c]);
> }
>
>
>