protected boolean isSquare(int n){ int sq = (int) Math.sqrt(n); return n == sq * sq; }
publicintnumSquares(int n){ // four-square and three-square theorems. while (n % 4 == 0) n /= 4; if (n % 8 == 7) return4;
if (this.isSquare(n)) return1; // enumeration to check if the number can be decomposed into sum of two squares. for (int i = 1; i * i <= n; ++i) { if (this.isSquare(n - i * i)) return2; } // bottom case of three-square theorem. return3; } }