Abstract In this paper we study the multiplicative function ρ k , λ (n) that counts the number of solutions of the equation x 1 2 + ⋯ + x k 2 ≡ λ (mod n) in (Z / n Z) k. In particular we give closed explicit formulas for ρ k , λ (p s). This leads to an algorithm with an arithmetic complexity of constant order that improves previous work by Tóth [10] and completes the quadratic case considered by Li and Ouyang in [8]. [ABSTRACT FROM AUTHOR]