Sub topic 17
Counting Integer Solutions Without Off-by-One Errors
This chapter assumes you can already solve linear, quadratic, rational and modulus inequalities, and that you can write the answers in interval notation. Here we deal with the last step, which is where many CAT answers go wrong: turning a solution set into a count of integers. The algebra is usually correct. The lost marks come from endpoints, excluded points, and gaps between intervals.
The Core Counting Rules
Suppose a and b are integers with a < b. The number of integers in each type of interval is:
| Interval | Meaning | Integer count | Example | Count |
|---|---|---|---|---|
| [a, b] | both ends included | b - a + 1 | [-3, 4] | 8 |
| [a, b) or (a, b] | one end included | b - a | (3, 10] | 7 |
| (a, b) | both ends excluded | b - a - 1 | (-4, 5) | 8 |
The base case is the closed interval: list the integers from a to b and you get b - a + 1. Each excluded integer endpoint takes away one.
When endpoints are not integers
If the endpoints are irrational or fractional, "open" or "closed" stops mattering, because the endpoint is not an integer in either case. Round inward instead:
- Smallest integer in the set:
ceil(a)(ifais an integer and excluded,a + 1). - Largest integer in the set:
floor(b)(ifbis an integer and excluded,b - 1). - Count = largest - smallest + 1, or 0 if this comes out negative.
For example, x^2 < 20 gives -√20 < x < √20, and √20 ≈ 4.47. The integers run from -4 to 4, so there are 9.
A Procedure That Avoids the Usual Slips
The last step takes about ten seconds and catches nearly every off-by-one error.
Worked Example 1: Two Modulus Terms
Count the integers satisfying |x - 3| + |x + 2| < 9.
The critical points are x = -2 and x = 3.
- For
x ≥ 3:(x - 3) + (x + 2) = 2x - 1 < 9, sox < 5. - For
-2 ≤ x < 3:(3 - x) + (x + 2) = 5 < 9, which always holds. - For
x < -2:(3 - x) - (x + 2) = 1 - 2x < 9, sox > -4.
The solution is (-4, 5), so the count is 5 - (-4) - 1 = 8. Check the boundaries: at x = 5 the sum is 2 + 7 = 9, which is not less than 9, so 5 is excluded. At x = 4 the sum is 1 + 6 = 7, which works.
There is also a shortcut that uses distance. The sum of distances from -2 and 3 is at least 5 everywhere. It equals 9 when x lies 2 units beyond either point, at -4 and 5. Everything strictly between those two points works.
Worked Example 2: Rational Inequality With an Excluded Point
How many integers in [-10, 10] satisfy (x - 1)(x + 2)/(x - 3) ≥ 0?
The wavy curve method gives [-2, 1] ∪ (3, ∞). Intersect this with the outer range:
[-2, 1]contains1 - (-2) + 1 = 4integers.(3, 10]contains10 - 3 = 7integers.
The total is 11. A common mistake is to include x = 3, which makes the denominator zero. Another is to write [3, 10] and count 8.
Modulus Shortcuts
When k is a positive integer and a is an integer:
| Condition | Solution | Integer count | Column | Column |
|---|---|---|---|---|
| \ | x - a\ | < k | (a - k, a + k) | 2k - 1 |
| \ | x - a\ | ≤ k | [a - k, a + k] | 2k + 1 |
| \ | x - a\ | > k, x in [L, R] | outside the closed band | (R - L + 1) - (2k + 1) |
The complement row is useful. Count the integers you exclude, which is easy, and subtract from the total.
Two Variables: Lattice Points
A lattice point is a point whose coordinates are both integers. For a positive integer n:
|x| + |y| ≤ ncontains2n^2 + 2n + 1lattice points. Rowycontributes2(n - |y|) + 1points.|x| + |y| = ncontains4nlattice points (the boundary of the square).|x| + |y| < nis the same as|x| + |y| ≤ n - 1, so it has2n^2 - 2n + 1points.
For n = 3: 25 points in total, 12 on the boundary and 13 inside. Do not mix up these counts with the area, which is 2n^2 = 18.
Fixing one variable
For a linear condition such as "positive integers with 2x + 3y ≤ 20", fix the variable with the larger coefficient and count the other one.
| y | Condition on x | Count |
|---|---|---|
| 1 | 2x ≤ 17, x ≤ 8 | 8 |
| 2 | 2x ≤ 14, x ≤ 7 | 7 |
| 3 | 2x ≤ 11, x ≤ 5 | 5 |
| 4 | 2x ≤ 8, x ≤ 4 | 4 |
| 5 | 2x ≤ 5, x ≤ 2 | 2 |
| 6 | 2x ≤ 2, x ≤ 1 | 1 |
The total is 27. Stop at y = 6, because y = 7 already gives 3y = 21 > 20. Fixing the larger coefficient gives fewer rows to work through.
Verifying With Code
The script below puts the rounding rules into functions and checks every example in this chapter by brute force.
import math
def count_open(a, b):
"""Number of integers n with a < n < b."""
return max(0, math.ceil(b) - math.floor(a) - 1)
def count_closed(a, b):
"""Number of integers n with a <= n <= b."""
return max(0, math.floor(b) - math.ceil(a) + 1)
# Interval formulas
print(count_open(2, 3)) # 0 (x^2 - 5x + 6 < 0)
print(count_open(-4, 5)) # 8
print(count_open(-math.sqrt(20), math.sqrt(20))) # 9 (x^2 < 20)
# Brute-force checks
print(sum(1 for x in range(-100, 101) if abs(x - 3) + abs(x + 2) < 9)) # 8
rational = sum(1 for x in range(-10, 11)
if x != 3 and (x - 1) * (x + 2) / (x - 3) >= 0)
print(rational) # 11
n = 3
print(sum(1 for x in range(-n, n + 1) for y in range(-n, n + 1)
if abs(x) + abs(y) <= n)) # 25
print(sum(1 for x in range(1, 21) for y in range(1, 21)
if 2 * x + 3 * y <= 20)) # 27Common Errors at a Glance
- You counted an open interval
(a, b)asb - a. - You included a point that makes the denominator zero.
- You forgot that a repeated root such as
(x - 2)^2in a≥ 0inequality adds an isolated solutionx = 2. In a> 0inequality it removes that point. - You ignored an outer restriction such as "positive integers" or "natural numbers less than 50".
- You added overlapping intervals without subtracting the integers they share.
- You reported the area of a region when the question asked for lattice points.
Exercise
How many integers satisfy |2x - 5| ≤ 7?
Show answer
-7 ≤ 2x - 5 ≤ 7 gives -1 ≤ x ≤ 6, so there are 6 - (-1) + 1 = 8 integers.
Exercise
How many integers x satisfy (x + 4)(x - 1)^2(x - 6) < 0?
Show answer
(x - 1)^2 is positive except at x = 1, where the expression is 0 and fails the strict inequality. (x + 4)(x - 6) < 0 gives -4 < x < 6, which contains 9 integers. Removing x = 1 leaves 8.
Exercise
How many lattice points lie strictly inside the region |x| + |y| ≤ 5, that is, satisfy |x| + |y| < 5?
Show answer
The condition is the same as |x| + |y| ≤ 4, so the count is 2(16) + 2(4) + 1 = 41. As a check, 2(25) + 10 + 1 = 61 total points minus 4(5) = 20 boundary points also gives 41.
Exercise
How many ordered pairs of positive integers (x, y) satisfy 3x + 4y < 30?
Show answer
Fix y. y = 1: 3x < 26, x ≤ 8, giving 8. y = 2: 3x < 22, x ≤ 7, giving 7. y = 3: 3x < 18, x ≤ 5, giving 5. y = 4: 3x < 14, x ≤ 4, giving 4. y = 5: 3x < 10, x ≤ 3, giving 3. y = 6: 3x < 6, x ≤ 1, giving 1. y = 7: 3x < 2, giving none. The total is 28.