Bill Allombert on Mon, 04 Dec 2023 11:35:24 +0100


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

Re: number of ways of writing a nonnegative integer n as a sum of 3 squares (zero being allowed).


On Sun, Dec 03, 2023 at 11:03:08PM -0800, Thomas D. Dean wrote:
> Sorry if this too far off topic.
> 
> I have been looking at OEIS, A005875, and can not understand their counting.
> I can see the Pari code and understand how it works.
> 
> OEIS calls this "number of ways of writing a nonnegative integer n as a sum
> of 3 squares (zero being allowed)." or "Number of ordered triples (i, j, k)
> of integers such that n = i^2 + j^2 + k^2." So, i,j,and, k should be
> considered distinct even if they are numerically equal?

I can only assume that by 'ordered triples' they just means vectors of length 3,
and not that the values are ordered.

Try this:
fun(n)=my(a=sqrtint(n),c=0);for(i=-a,a,for(j=-a,a,for(k=-a,a,if(i^2+j^2+k^2==n,print(c++,":",[i,j,k])))));

? fun(1)
1:[-1,0,0]
2:[0,-1,0]
3:[0,0,-1]
4:[0,0,1]
5:[0,1,0]
6:[1,0,0]

? fun(2)
1:[-1,-1,0]
2:[-1,0,-1]
3:[-1,0,1]
4:[-1,1,0]
5:[0,-1,-1]
6:[0,-1,1]
7:[0,1,-1]
8:[0,1,1]
9:[1,-1,0]
10:[1,0,-1]
11:[1,0,1]
12:[1,1,0]

etc.
Cheers,
Bill.