Integers and Primes
Integer Divisibility
Whenever a program checks whether a number is even, splits records into equal batches or maps a key to a hash bucket, it is asking a divisibility question. This lesson makes that question precise. It covers what it means for one integer to divide another, how every integer splits into a quotient and a remainder, and why the % operator in your language may disagree with the mathematics when numbers are negative.
When one integer divides another
For integers a and b with a=0, a divides b when there is an integer k such that
You write a∣b and say that b is a multiple of a, or that a is a divisor of b. When no such integer k exists, you write a∤b.
Example: 6∣42 because 42=6⋅7, and −4∣28 because 28=(−4)(−7). However, 6∤45: the only number k with 45=6k is 7.5, which is not an integer.
Three consequences of the definition catch students out:
- Every nonzero integer divides 0, because 0=a⋅0.
- Signs do not matter: a∣b holds exactly when −a∣b holds, and exactly when a∣−b holds.
- a∣b is a statement that is either true or false, not a number. Do not confuse it with the fraction b/a.
The division algorithm
Divisibility is a special case of a more general fact. The division algorithm states that for any integer a and any positive integer d, there is exactly one pair of integers q (the quotient) and r (the remainder) such that
The quotient is the floor q=⌊a/d⌋, and the remainder is r=a−dq. So d∣a exactly when the remainder is 0.
Example: For a=−17 and d=5, you get −17/5=−3.4, so q=⌊−3.4⌋=−4 and r=−17−5(−4)=3. Check: 5(−4)+3=−17 and 0≤3<5. Writing −17=5(−3)−2 gives a true equation, but its r=−2 breaks the condition 0≤r<d.
Programming languages split at exactly this point. Python's // and % use the floor, so for d>0 they match the division algorithm. C, C++ and Java truncate the quotient toward zero, so their % result takes the sign of the dividend.
a | d | q | r | Python a % d | Java a % d |
|---|---|---|---|---|---|
17 | 5 | 3 | 2 | 2 | 2 |
−17 | 5 | −4 | 3 | 3 | −2 |
20 | 5 | 4 | 0 | 0 | 0 |
−20 | 5 | −4 | 0 | 0 | 0 |
The last two rows show why a divisibility test that checks whether a % d is zero works in every language: a zero remainder is zero under either rule.
Combining divisibility facts
Two properties follow directly from the definition and carry most proofs about divisibility:
- Linear combinations: if a∣b and a∣c, then a∣(bx+cy) for all integers x and y.
- Transitivity: if a∣b and b∣c, then a∣c.
The proof of the first is the template for the rest. Write b=ak and c=al with k and l integers. Then bx+cy=a(kx+ly), and kx+ly is an integer, so a divides the combination. Transitivity works the same way: b=ak and c=bm give c=a(km).
Example: Since 9∣27 and 9∣45, the property guarantees 9∣(2⋅27−45), and indeed 54−45=9.
The reverse direction does not hold. From a∣(b+c) you cannot conclude a∣b: 4∣(1+3), yet 4∤1. Likewise a∣bc does not imply a∣b or a∣c: 6∣36=4⋅9, yet 6 divides neither 4 nor 9. When a is prime that last implication does hold, a result you meet when you study primes.
What to remember
- a∣b means b=ak for some integer k; it is a true-or-false statement, not a fraction.
- For d>0, every integer a has exactly one quotient q=⌊a/d⌋ and remainder r with a=dq+r and 0≤r<d.
- d∣a exactly when that remainder is 0, so a zero test on a % d works in any language.
- Python's % matches the division algorithm for d>0; Java, C and C++ can return negative remainders.
- Divisibility carries over to linear combinations and is transitive, but it does not run in reverse.
Example: Hash bucket indices for negative keys
A hash table stores records in 12 buckets numbered 0 to 11. A record with integer key k goes into bucket r, the remainder the division algorithm gives for k with divisor 12, so 0≤r<12. Some keys are signed sensor offsets, so they can be negative.
a) For the key k=−50, find the quotient q and the remainder r, and state the bucket.
Write −50=12q+r with 0≤r<12. Dividing gives −50/12≈−4.17, and the quotient is the floor of that value, which is −5 because the floor moves down, away from zero, for a negative number.
Multiplying back, 12 × (−5) = ________, so the remainder is
Check: 12(−5)+10=−50 and 0≤10<12. The key −50 goes into bucket 10.
b) The table is written in Java, where k % 12 truncates the quotient toward zero. What does -50 % 12 return, and how can the code correct it to the bucket from part a)?
Truncating −4.17 toward zero gives a quotient of ________. Multiplying back, 12 × (−4) = ________, so Java computes
So -50 % 12 returns −2. That is not a valid bucket number, and using it as an array index throws an exception. Add 12 and reduce again:
which matches part a). The expression ((k % 12) + 12) % 12 does this for every key, and Java's Math.floorMod(k, 12) returns the same value directly. The final reduction matters for keys whose Java remainder is already non-negative: 26 % 12 is 2, and without the last step the formula would give 14, outside the table.
c) Keys x=84 and y=−36 both land in bucket 0. A rebalancing step stores the combined key 5x−3y. Show that this key also lands in bucket 0, and explain why the same happens for any two keys in bucket 0.
Bucket 0 means remainder 0, so 12 divides each key: 84 = 12 × ________ and −36 = 12 × (________). Computing the two terms, 5x is ________ and −3y is ________, so
The remainder is 0, so the combined key lands in bucket 0.
For the general case, suppose 12∣x and 12∣y, and write x=12m and y=12n with m and n integers. Then
and 5m−3n is an integer, so 12∣(5x−3y). This is the linear-combination property with coefficients 5 and −3. For these keys m=7 and n=−3, giving 5m−3n=35+9=44, which matches the direct calculation.