cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A002654 Number of ways of writing n as a sum of at most two nonzero squares, where order matters; also (number of divisors of n of form 4m+1) - (number of divisors of form 4m+3).

This page as a plain text file.
%I A002654 M0012 N0001 #183 Jul 25 2025 15:42:08
%S A002654 1,1,0,1,2,0,0,1,1,2,0,0,2,0,0,1,2,1,0,2,0,0,0,0,3,2,0,0,2,0,0,1,0,2,
%T A002654 0,1,2,0,0,2,2,0,0,0,2,0,0,0,1,3,0,2,2,0,0,0,0,2,0,0,2,0,0,1,4,0,0,2,
%U A002654 0,0,0,1,2,2,0,0,0,0,0,2,1,2,0,0,4,0,0,0,2,2,0,0,0,0,0,0,2,1,0,3,2,0,0,2,0
%N A002654 Number of ways of writing n as a sum of at most two nonzero squares, where order matters; also (number of divisors of n of form 4m+1) - (number of divisors of form 4m+3).
%C A002654 Glaisher calls this E(n) or E_0(n). - _N. J. A. Sloane_, Nov 24 2018
%C A002654 Number of sublattices of Z X Z of index n that are similar to Z X Z; number of (principal) ideals of Z[i] of norm n.
%C A002654 a(n) is also one fourth of the number of integer solutions of n = x^2 + y^2 (order and signs matter, and 0 (without signs) is allowed). a(n) = N(n)/4, with N(n) from p. 147 of the Niven-Zuckermann reference. See also Theorem 5.12, p. 150, which defines a (strongly) multiplicative function h(n) which coincides with A056594(n-1), n >= 1, and N(n)/4 = sum(h(d), d divides n). - _Wolfdieter Lang_, Apr 19 2013
%C A002654 a(2+8*N) = A008441(N) gives the number of ways of writing N as the sum of 2 (nonnegative) triangular numbers for N >= 0. - _Wolfdieter Lang_, Jan 12 2017
%C A002654 Coefficients of Dedekind zeta function for the quadratic number field of discriminant -4. See A002324 for formula and Maple code. - _N. J. A. Sloane_, Mar 22 2022
%D A002654 J. M. Borwein, D. H. Bailey and R. Girgensohn, Experimentation in Mathematics, A K Peters, Ltd., Natick, MA, 2004. x+357 pp. See p. 194.
%D A002654 George Chrystal, Algebra: An elementary text-book for the higher classes of secondary schools and for colleges, 6th ed., Chelsea Publishing Co., New York, 1959, Part II, p. 346 Exercise XXI(17). MR0121327 (22 #12066)
%D A002654 Emil Grosswald, Representations of Integers as Sums of Squares. Springer-Verlag, NY, 1985, p. 15.
%D A002654 Ivan Niven and Herbert S. Zuckerman, An Introduction to the Theory of Numbers, New York: John Wiley, 1980, pp. 147 and 150.
%D A002654 Günter Scheja and Uwe Storch, Lehrbuch der Algebra, Tuebner, 1988, p. 251.
%D A002654 N. J. A. Sloane, A Handbook of Integer Sequences, Academic Press, 1973 (includes this sequence).
%D A002654 N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
%D A002654 James J. Tattersall, Elementary Number Theory in Nine Chapters, Cambridge University Press, 1999, page 89.
%D A002654 J. V. Uspensky and M. A. Heaslet, Elementary Number Theory, McGraw-Hill, NY, 1939, p. 340.
%H A002654 T. D. Noe, <a href="/A002654/b002654.txt">Table of n, a(n) for n = 1..10000</a>
%H A002654 Michael Baake, Solution of the coincidence problem in dimensions d <= 4, in R. V. Moody, ed., The Mathematics of Long-Range Aperiodic Order, Kluwer 1997, pp. 9-44; arXiv:<a href="https://arxiv.org/abs/math/0605222">math/0605222</a> [math.MG], 2006.
%H A002654 Michael Baake and Uwe Grimm, <a href="http://www.ma.utexas.edu/mp_arc-bin/mpa?yn=02-392">Quasicrystalline combinatorics</a>, 2002.
%H A002654 Shai Covo, <a href="https://cms.math.ca/publications/crux/issue?volume=36&amp;issue=7">Problem 3586</a>, Crux Mathematicorum, Vol. 36, No. 7 (2010), pp. 461 and 463; <a href="https://cms.math.ca/publications/crux/issue?volume=37&amp;issue=7">Solution to Problem 3586</a> by the proposer, ibid., Vol. 37, No. 7 (2011), pp. 477-479.
%H A002654 J. W. L. Glaisher, <a href="http://gdz.sub.uni-goettingen.de/en/dms/loader/img/?PID=PPN600494829_0020%7CLOG_0017">On the function chi(n)</a>, Quarterly Journal of Pure and Applied Mathematics, Vol. 20 (1884), pp. 97-167.
%H A002654 J. W. L. Glaisher, <a href="/A002171/a002171.pdf">On the function chi(n)</a>, Quarterly Journal of Pure and Applied Mathematics, Vol. 20 (1884), pp. 97-167. [Annotated scanned copy]
%H A002654 J. W. L. Glaisher, <a href="https://doi.org/10.1112/plms/s1-15.1.104">On the function which denotes the difference between the number of (4m+1)-divisors and the number of (4m+3)-divisors of a number</a>, Proc. London Math. Soc., Vol. 15 (1884), pp. 104-122.
%H A002654 J. W. L. Glaisher, <a href="/A002654/a002654.pdf">On the function which denotes the difference between the number of (4m+1)-divisors and the number of (4m+3)-divisors of a number</a>, Proc. London Math. Soc., Vol. 15 (1884), pp. 104-122. [Annotated scanned copy of pages 104-107 only]
%H A002654 J. W. L. Glaisher, <a href="https://books.google.com/books?id=bLs9AQAAMAAJ&amp;pg=RA1-PA1">On the representations of a number as the sum of two, four, six, eight, ten, and twelve squares</a>, Quart. J. Math., Vol. 38 (1907), pp. 1-62 (see p. 4 and p. 8).
%H A002654 Christian Kassel and Christophe Reutenauer, <a href="https://arxiv.org/abs/2507.15780">Pairs of intertwined integer sequences</a>, arXiv:2507.15780 [math.NT], 2025. See p. 5.
%H A002654 Vaclav Kotesovec, <a href="/A002654/a002654.jpg">Graph - the asymptotic ratio of Sum_{k=1..n} a(k)^2</a>
%H A002654 Stephen C. Milne, <a href="http://dx.doi.org/10.1023/A:1014865816981">Infinite families of exact sums of squares formulas, Jacobi elliptic functions, continued fractions and Schur functions</a>, Ramanujan J., Vol. 6 (2002), pp. 7-149.
%H A002654 PeakMath, <a href="https://www.youtube.com/watch?v=4bzSFNCiKrk">L-functions and the Langlands program (RH Saga S1E2)</a>, YouTube video (2024).
%H A002654 Srinivasa Ramanujan, <a href="http://ramanujan.sirinudi.org/Volumes/published/ram17.html">Some formulas in the analytic theory of numbers</a>, Messenger of Mathematics, XLV, 1916, 81-84, section (K).
%H A002654 John S. Rutherford, <a href="http://dx.doi.org/10.1107/S010876730804333X">Sublattice enumeration. IV. Equivalence classes of plane sublattices by parent Patterson symmetry and colour lattice group type</a>, Acta Cryst. (2009). A65, 156-163. [See Table 1]. - From _N. J. A. Sloane_, Feb 23 2009
%H A002654 <a href="/index/Su#ssq">Index entries for sequences related to sums of squares</a>.
%H A002654 <a href="/index/Su#sublatts">Index entries for sequences related to sublattices</a>.
%H A002654 <a href="/index/Cor#core">Index entries for "core" sequences</a>.
%H A002654 <a href="/index/Ge#Glaisher">Index entries for sequences mentioned by Glaisher</a>.
%F A002654 Dirichlet series: (1-2^(-s))^(-1)*Product (1-p^(-s))^(-2) (p=1 mod 4) * Product (1-p^(-2s))^(-1) (p=3 mod 4) = Dedekind zeta-function of Z[ i ].
%F A002654 Coefficients in expansion of Dirichlet series Product_p (1-(Kronecker(m, p)+1)*p^(-s)+Kronecker(m, p)*p^(-2s))^(-1) for m = -16.
%F A002654 If n=2^k*u*v, where u is product of primes 4m+1, v is product of primes 4m+3, then a(n)=0 unless v is a square, in which case a(n) = number of divisors of u (Jacobi).
%F A002654 Multiplicative with a(p^e) = 1 if p = 2; e+1 if p == 1 (mod 4); (e+1) mod 2 if p == 3 (mod 4). - _David W. Wilson_, Sep 01 2001
%F A002654 G.f. A(x) satisfies 0 = f(A(x), A(x^2), A(x^4)) where f(u, v, w) = (u - v)^2 - (v - w) * (4*w + 1). - _Michael Somos_, Jul 19 2004
%F A002654 G.f.: Sum_{n>=1} ((-1)^floor(n/2)*x^((n^2+n)/2)/(1+(-x)^n)). - _Vladeta Jovovic_, Sep 15 2004
%F A002654 Expansion of (eta(q^2)^10 / (eta(q) * eta(q^4))^4 - 1)/4 in powers of q.
%F A002654 G.f.: Sum_{k>0} x^k / (1 + x^(2*k)) = Sum_{k>0} -(-1)^k * x^(2*k - 1) / (1 - x^(2*k - 1)). - _Michael Somos_, Aug 17 2005
%F A002654 a(4*n + 3) = a(9*n + 3) = a(9*n + 6) = 0. a(9*n) = a(2*n) = a(n). - _Michael Somos_, Nov 01 2006
%F A002654 a(4*n + 1) = A008441(n). a(3*n + 1) = A122865(n). a(3*n + 2) = A122856(n). a(12*n + 1) = A002175(n). a(12*n + 5) = 2 * A121444(n). 4 * a(n) = A004018(n) unless n=0.
%F A002654 a(n) = Sum_{k=1..n} A010052(k)*A010052(n-k). a(A022544(n)) = 0; a(A001481(n)) > 0.
%F A002654 - _Reinhard Zumkeller_, Sep 27 2008
%F A002654 a(n) = A001826(n) - A001842(n). - _R. J. Mathar_, Mar 23 2011
%F A002654 a(n) = Sum_{d|n} A056594(d-1), n >= 1. See the above comment on A056594(d-1) = h(d) of the Niven-Zuckerman reference. - _Wolfdieter Lang_, Apr 19 2013
%F A002654 Dirichlet g.f.: zeta(s)*beta(s) = zeta(s)*L(chi_2(4),s). - _Ralf Stephan_, Mar 27 2015
%F A002654 G.f.: (theta_3(x)^2 - 1)/4, where theta_3() is the Jacobi theta function. - _Ilya Gutkovskiy_, Apr 17 2018
%F A002654 a(n) = Sum_{ m: m^2|n } A000089(n/m^2). - _Andrey Zabolotskiy_, May 07 2018
%F A002654 a(n) = A053866(n) + 2 * A025441(n). - _Andrey Zabolotskiy_, Apr 23 2019
%F A002654 a(n) = Im(Sum_{d|n} i^d). - _Ridouane Oudra_, Feb 02 2020
%F A002654 a(n) = Sum_{d|n} sin((1/2)*d*Pi). - _Ridouane Oudra_, Jan 22 2021
%F A002654 Sum_{n>=1} (-1)^n*a(n)/n = Pi*log(2)/4 (Covo, 2010). - _Amiram Eldar_, Apr 07 2022
%F A002654 Asymptotic mean: Limit_{m->oo} (1/m) * Sum_{k=1..m} a(k) = Pi/4 = 0.785398... (A003881). - _Amiram Eldar_, Oct 11 2022
%F A002654 From _Vaclav Kotesovec_, Mar 10 2023: (Start)
%F A002654 Sum_{k=1..n} a(k)^2 ~ n * (log(n) + C) / 4, where C = A241011 =
%F A002654 4*gamma - 1 + log(2)/3 - 2*log(Pi) + 8*log(Gamma(3/4)) - 12*Zeta'(2)/Pi^2 = 2.01662154573340811526279685971511542645018417752364748061...
%F A002654 The constant C, published by Ramanujan (1916, formula (22)), 4*gamma - 1 + log(2)/3 - log(Pi) + 4*log(Gamma(3/4)) - 12*Zeta'(2)/Pi^2 = 2.3482276258576... is wrong! (End)
%e A002654 4 = 2^2, so a(4) = 1; 5 = 1^2 + 2^2 = 2^2 + 1^2, so a(5) = 2.
%e A002654 x + x^2 + x^4 + 2*x^5 + x^8 + x^9 + 2*x^10 + 2*x^13 + x^16 + 2*x^17 + x^18 + ...
%e A002654 2 = (+1)^2 + (+1)^2 = (+1)^2 + (-1)^2  = (-1)^2 + (+1)^2 = (-1)^2 + (-1)^2. Hence there are 4 integer solutions, called N(2) in the Niven-Zuckerman reference, and a(2) = N(2)/4 = 1.  4 = 0^1 + (+2)^2 = (+2)^2 + 0^2 = 0^2 + (-2)^2 = (-2)^2 + 0^2. Hence N(4) = 4 and a(4) = N(4)/4 = 1. N(5) = 8, a(5) = 2. - _Wolfdieter Lang_, Apr 19 2013
%p A002654 with(numtheory):
%p A002654 A002654 := proc(n)
%p A002654     local count1, count3, d;
%p A002654     count1 := 0:
%p A002654     count3 := 0:
%p A002654     for d in numtheory[divisors](n) do
%p A002654         if d mod 4 = 1 then
%p A002654             count1 := count1+1
%p A002654         elif d mod 4 = 3 then
%p A002654             count3 := count3+1
%p A002654         fi:
%p A002654     end do:
%p A002654     count1-count3;
%p A002654 end proc:
%p A002654 # second Maple program:
%p A002654 a:= n-> add(`if`(d::odd, (-1)^((d-1)/2), 0), d=numtheory[divisors](n)):
%p A002654 seq(a(n), n=1..100);  # _Alois P. Heinz_, Feb 04 2020
%t A002654 a[n_] := Count[Divisors[n], d_ /; Mod[d, 4] == 1] - Count[Divisors[n], d_ /; Mod[d, 4] == 3]; a/@Range[105] (* _Jean-François Alcover_, Apr 06 2011, after _R. J. Mathar_ *)
%t A002654 QP = QPochhammer; CoefficientList[(1/q)*(QP[q^2]^10/(QP[q]*QP[q^4])^4-1)/4 + O[q]^100, q] (* _Jean-François Alcover_, Nov 24 2015 *)
%t A002654 f[2, e_] := 1; f[p_, e_] := If[Mod[p, 4] == 1, e + 1, Mod[e + 1, 2]]; a[1] = 1; a[n_] := Times @@ f @@@ FactorInteger[n]; Array[a, 100] (* _Amiram Eldar_, Sep 19 2020 *)
%t A002654 Rest[CoefficientList[Series[EllipticTheta[3, 0, q]^2/4, {q, 0, 100}], q]] (* _Vaclav Kotesovec_, Mar 10 2023 *)
%o A002654 (PARI) direuler(p=2,101,1/(1-X)/(1-kronecker(-4,p)*X))
%o A002654 (PARI) {a(n) = polcoeff( sum(k=1, n, x^k / (1 + x^(2*k)), x * O(x^n)), n)}
%o A002654 (PARI) {a(n) = sumdiv( n, d, (d%4==1) - (d%4==3))}
%o A002654 (PARI) {a(n) = local(A); A = x * O(x^n); polcoeff( eta(x^2 + A)^10 / (eta(x + A) * eta(x^4 + A))^4 / 4, n)} \\ _Michael Somos_, Jun 03 2005
%o A002654 (PARI) a(n)=my(f=factor(n>>valuation(n,2))); prod(i=1,#f~, if(f[i,1]%4==1, f[i,2]+1, (f[i,2]+1)%2)) \\ _Charles R Greathouse IV_, Sep 09 2014
%o A002654 (PARI) my(B=bnfinit(x^2+1)); vector(100,n,#bnfisintnorm(B,n)) \\ _Joerg Arndt_, Jun 01 2024
%o A002654 (Haskell)
%o A002654 a002654 n = product $ zipWith f (a027748_row m) (a124010_row m) where
%o A002654    f p e | p `mod` 4 == 1 = e + 1
%o A002654          | otherwise      = (e + 1) `mod` 2
%o A002654    m = a000265 n
%o A002654 -- _Reinhard Zumkeller_, Mar 18 2013
%o A002654 (Python)
%o A002654 from math import prod
%o A002654 from sympy import factorint
%o A002654 def A002654(n): return prod(1 if p == 2 else (e+1 if p % 4 == 1 else (e+1) % 2) for p, e in factorint(n).items()) # _Chai Wah Wu_, May 09 2022
%Y A002654 Cf. A000161, A001481, A003881.
%Y A002654 Equals 1/4 of A004018. Partial sums give A014200.
%Y A002654 Cf. A002175, A008441, A121444, A122856, A122865, A022544, A143574, A000265, A027748, A124010, A025426 (two squares, order does not matter), A120630 (Dirichlet inverse), A101455 (Mobius transform), A000089, A241011.
%Y A002654 If one simply reads the table in Glaisher, PLMS 1884, which omits the zero entries, one gets A213408.
%Y A002654 Dedekind zeta functions for imaginary quadratic number fields of discriminants -3, -4, -7, -8, -11, -15, -19, -20 are A002324, A002654, A035182, A002325, A035179, A035175, A035171, A035170, respectively.
%Y A002654 Dedekind zeta functions for real quadratic number fields of discriminants 5, 8, 12, 13, 17, 21, 24, 28, 29, 33, 37, 40 are A035187, A035185, A035194, A035195, A035199, A035203, A035188, A035210, A035211, A035215, A035219, A035192, respectively.
%K A002654 core,easy,nonn,nice,mult
%O A002654 1,5
%A A002654 _N. J. A. Sloane_