2. I want a closed form for S(n) = 1^2 + 2^2 + ... + n^2 for every nonnegative integer n. The empty sum S(0) is zero. Rather than guessing a formula from a few values, I will derive it from a telescoping identity and then check it independently by induction. The labels here mark checkpoints in the argument; they are deliberately spaced rather than counting the terms of the sum. The quantity being summed is a square, so a cubic difference is a promising place to look: subtracting neighboring cubes produces a quadratic expression.

5. Expand (k + 1)^3 using ordinary multiplication. The result is k^3 + 3k^2 + 3k + 1, so (k + 1)^3 - k^3 = 3k^2 + 3k + 1. The square term appears with coefficient three, alongside a linear term and a constant. Summing this identity over a consecutive set of values should collapse the left side, while the right side contains the square sum we want plus two simpler sums. No approximation is involved; the identity holds separately for every integer k.

9. Sum from k = 1 through k = n. On the left, the expanded sequence is (2^3 - 1^3) + (3^3 - 2^3) + ... + ((n + 1)^3 - n^3). Every interior cube appears once with a plus sign and once with a minus sign. What remains is (n + 1)^3 - 1. On the right, distribute the finite sum over addition to obtain 3S(n) + 3(1 + 2 + ... + n) + n. The last n comes from adding the constant one exactly n times.

14. The linear sum is n(n + 1)/2. To see that without importing another unexplained formula, write the same sum in forward and reverse order. Pairing corresponding terms gives n pairs, each equal to n + 1. Thus twice the sum is n(n + 1). This proof also works when n is odd: it pairs two copies of the complete sum rather than claiming that the original list alone splits into pairs. Substituting this result into the telescoping equation gives (n + 1)^3 - 1 = 3S(n) + 3n(n + 1)/2 + n.

20. Isolate the unknown square sum. We have 3S(n) = (n + 1)^3 - 1 - n - 3n(n + 1)/2. Expanding the cube and combining its constant with the negative one leaves n^3 + 3n^2 + 2n before the final subtraction. Subtracting three halves of n^2 + n then gives n^3 + (3/2)n^2 + (1/2)n. Therefore 3S(n) = n(2n^2 + 3n + 1)/2. The quadratic factor is (n + 1)(2n + 1), so dividing by three yields S(n) = n(n + 1)(2n + 1)/6.

27. I should check the lower boundary separately because the telescoping display began at one. At n = 0, the proposed expression has a factor n and is zero, exactly matching the empty sum. At n = 1, it gives 1 times 2 times 3 divided by 6, which is one. At n = 3, it gives 3 times 4 times 7 divided by 6, which is fourteen, matching 1 + 4 + 9. These checks do not prove the formula, but they are useful for finding an omitted endpoint or an incorrect constant in a telescoping derivation.

35. For an independent induction check, suppose the expression is correct at n. Adding the next square gives S(n + 1) = n(n + 1)(2n + 1)/6 + (n + 1)^2. Factor out (n + 1)/6 to obtain (n + 1)[n(2n + 1) + 6(n + 1)]/6. The bracket simplifies to 2n^2 + 7n + 6, which factors as (n + 2)(2n + 3). Thus the new result is (n + 1)(n + 2)(2n + 3)/6, precisely the proposed formula with n replaced by n + 1.

44. The base case and this induction step establish the formula for all nonnegative integers. Notice the different roles of the two arguments. Telescoping explains where the cubic expression comes from, while induction verifies that the final expression has the correct initial value and changes by exactly the next square. They reinforce each other without depending on a numerical pattern. A list of the first several sums could suggest a cubic, but many other functions agree on a finite sample. The exact difference relation is what identifies the formula over the entire domain.

54. The division by six always produces an integer, even though the algebra used fractions along the way. One of n and n + 1 is even, so their product contributes a factor two. For divisibility by three, consider n modulo three. If n is zero modulo three, the factor n supplies three. If n is two modulo three, n + 1 supplies it. If n is one modulo three, 2n + 1 is zero modulo three. Combining the factors two and three proves divisibility by six, since they are relatively prime. This also confirms that integer division is appropriate in a Python implementation.

65. If the intended sum starts at an integer a and ends at b, with 1 <= a <= b, the same result immediately gives a range formula: a^2 + ... + b^2 = S(b) - S(a - 1). The subtraction removes exactly the terms before a and leaves the rest. There is no need to derive a second polynomial. For an empty range with a > b, a caller should decide whether the interface returns zero or rejects the range rather than relying on the subtraction formula outside its stated bounds. The basic prefix-sum identity is unambiguous for the valid ordered range.

77. The leading term is n^3/3, followed by n^2/2 and n/6. This is consistent with the rough size of the sum: there are n terms, and the largest is n^2, so the total is at most n^3. It should also grow on a cubic scale because a positive fraction of the terms are themselves on the scale of n^2. These observations are only magnitude checks; they cannot determine the lower-order terms. The derivation above supplies those terms exactly and explains why using only n^3/3 would miss a substantial finite correction.

90. In Python, ordinary integers can represent the exact product, so the function can multiply the three factors and use // 6. A fixed-width language would need additional care about intermediate overflow even when the final quotient fits. Dividing an even factor by two and an appropriate factor by three before multiplication can reduce that intermediate size. That implementation issue does not alter the mathematical formula. For this answer, the Python version is the simplest faithful translation, and rejecting negative n keeps the domain aligned with the finite sum that was defined at the beginning.

104. I have the result, but I want to make sure the factorization did not conceal an indexing convention. A second exact derivation can use the identity k^2 = 2C(k, 2) + C(k, 1). The right side equals k(k - 1) + k, so it really is k^2, including k = 1 if C(1, 2) is read as zero. Summing it separates the square sum into two binomial-coefficient sums. The first counts pairs chosen from each prefix, and the second counts single elements chosen from each prefix. These have a direct interpretation that keeps the endpoints visible.

119. For the pair sum, sum C(k, 2) as k ranges from one through n. Count triples selected from one through n + 1 according to their greatest member. If the greatest member is k + 1, the other two members form a pair chosen from one through k, giving C(k, 2) possibilities. Every triple has a unique greatest member, so it belongs to exactly one of these cases. Thus the pair sum is C(n + 1, 3). The term for k = 1 contributes zero, which is why the apparently extra initial value creates no discrepancy.

135. The single-element sum similarly equals C(n + 1, 2). A pair whose larger member is k + 1 has k choices for its smaller member, and k is C(k, 1). Combining the two counts gives S(n) = 2C(n + 1, 3) + C(n + 1, 2). Expanding those binomial coefficients gives n(n + 1)(n - 1)/3 + n(n + 1)/2. Factor out n(n + 1)/6 and the remaining expression is 2(n - 1) + 3 = 2n + 1. This agrees with the earlier formula, with no shift from n to n + 1 missing.

152. There is also a useful geometric interpretation of the identity for one square. In a k by k array, separate the diagonal, containing k cells, from the cells strictly above and strictly below it. There are C(k, 2) cells on each side, because choosing two distinct indices identifies one above-diagonal cell and its reflected below-diagonal partner. Thus the array contains k + 2C(k, 2) cells. Stacking such arrays for k from one through n gives the same binomial expression for the square pyramid. This is a counting explanation of the algebraic decomposition, rather than a separate assumption about the result.

170. Another way to reconstruct the formula is to seek a cubic polynomial F(n) whose first difference is n^2 and whose value at zero is zero. Write F(n) = an^3 + bn^2 + cn; there is no constant because of the zero boundary condition. Then F(n) - F(n - 1) equals a(3n^2 - 3n + 1) + b(2n - 1) + c. Matching coefficients against n^2 gives 3a = 1, -3a + 2b = 0, and a - b + c = 0. Solving in that order yields a = 1/3, b = 1/2, and c = 1/6.

189. That coefficient calculation is more than curve fitting. The difference equation and initial condition determine all subsequent values uniquely: once F(n - 1) is known, F(n) must equal it plus n^2. Since the derived polynomial satisfies that equation identically, it agrees with the finite sum at every nonnegative integer. We did not need to assert that every imaginable function matching the sum is a polynomial. We only needed to construct one polynomial with the correct difference and boundary value. This distinction matters because finite numerical samples alone never impose such a global uniqueness claim.

209. I can use the formula to check a related expression whose parity makes indexing errors easy to spot: the sum of the first n odd squares. Those terms are (2k - 1)^2 for k from one through n. Expanding each term gives 4k^2 - 4k + 1. Summing produces 4S(n) - 4[n(n + 1)/2] + n. Substitute the square-sum formula and place everything over three. The result is n[2(n + 1)(2n + 1) - 6(n + 1) + 3]/3, which simplifies to n(4n^2 - 1)/3. This is a useful independent bookkeeping check on both lower-order coefficients.

230. For n = 2, the odd squares are one and nine, summing to ten. The derived companion expression gives 2(16 - 1)/3 = 10. For n = 3, it gives 3(36 - 1)/3 = 35, matching one plus nine plus twenty-five. The original square formula also satisfies a decomposition into even and odd contributions: S(2n) = 4S(n) + n(4n^2 - 1)/3. The even squares contribute four times S(n), and the odd squares contribute the companion sum. Expanding either side gives the same cubic in n, which checks the change from a count of n terms to an endpoint of 2n.

252. The precise input contract still deserves attention. The mathematical domain is a nonnegative integer; a negative integer cannot represent the number of terms as defined here. Python's comparisons and multiplication also accept some noninteger types, but allowing those implicitly would make the // operation ambiguous as an expression of the problem. In a small benchmark-style function, the caller's promise that n is an integer is normally sufficient. The code can reject negative values explicitly while documenting the integer requirement. A larger public API could additionally reject booleans and other noninteger values, but that validation policy is separate from this derivation.

275. A loop that accumulates total += k * k from one through n provides a straightforward reference implementation for small values. It takes n iterations and makes each term visible, so it is a useful way to compare examples. The closed form reduces the number of arithmetic operations to a constant, although the cost of multiplying large integers grows with the number of digits in n. Describing that as constant time assumes a unit-cost arithmetic model; for arbitrary-precision integers, the more precise statement is a constant number of big-integer operations. Either interpretation still avoids a loop whose iteration count grows with the numeric value of n.

299. The order of multiplication does not change exact Python arithmetic, and integer division after the complete product is safe because divisibility by six has already been proved. Replacing // with / would instead return a floating-point value, potentially losing exactness for large inputs. Rounding that floating result afterward would not recover digits already lost in conversion. There is also no need to add a tiny correction or depend on a decimal approximation to one sixth. The expression uses integer operations throughout, so its result matches the finite integer sum exactly for every supported n in the stated domain.

324. The final result is S(n) = n(n + 1)(2n + 1)/6 for n >= 0. The telescoping equation, induction, binomial counting, and first-difference calculation all agree on the same endpoints and coefficients. The empty case is included, and the companion odd-square check is consistent. I will use the compact integer expression rather than returning the reference loop; the derivation above supplies the reason that this shorter computation is exact. Here is the complete implementation, with a domain check and no floating-point intermediate:

```python
def sum_of_squares(n):
    if n < 0:
        raise ValueError("n must be nonnegative")
    return n * (n + 1) * (2 * n + 1) // 6
```
