FLINT3.7.0-dev

arith_sum_of_squares

Synopsis#include <flint/arith.h>
void arith_sum_of_squares(fmpz_t r, ulong k, const fmpz_t n)source

Sets rr to the number rk(n)r_k(n) of representations of nn as an ordered sum of kk squares of integers (signs and order count), with rk(0)=1r_k(0) = 1 and rk(n)=0r_k(n) = 0 for n<0n < 0.

For k=1k = 1 the result is 2 or 0. For k=2k = 2 and k=4k = 4 the function factors nn and uses Jacobi's formulas r2(n)=4∑d∣n, d odd(−1)(d−1)/2r_2(n) = 4 \sum_{d \mid n,\, d \text{ odd}} (-1)^{(d-1)/2} and r4(n)=8∑d∣n, 4∤ddr_4(n) = 8 \sum_{d \mid n,\, 4 \nmid d} d [Grosswald1985]. For k=3k = 3 and k=5k = 5 it uses the recursion rk(n)=∑j∈Zrk−1(n−j2)r_k(n) = \sum_{j \in \mathbb{Z}} r_{k-1}(n - j^2), with O(n)O(\sqrt n) calls for k−1∈{2,4}k - 1 \in \{2, 4\}; nn is then assumed to fit in a ulong. For other kk it computes the coefficients of ϑ3(q)k\vartheta_3(q)^k up to qnq^n with arith_sum_of_squares_vec, which requires nn to fit in a slong and throws otherwise.

References#

Cite this page

The FLINT team, “arith_sum_of_squares (arith.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/arith/arith_sum_of_squares.html (generated 2026-11-02).