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 aa and bb with a≠0a \neq 0, aa divides bb when there is an integer kk such that


b=ak.b = ak.


You write a∣ba \mid b and say that bb is a multiple of aa, or that aa is a divisor of bb. When no such integer kk exists, you write a∤ba \nmid b.


Example: 6∣426 \mid 42 because 42=6⋅742 = 6 \cdot 7, and −4∣28-4 \mid 28 because 28=(−4)(−7)28 = (-4)(-7). However, 6∤456 \nmid 45: the only number kk with 45=6k45 = 6k is 7.57.5, which is not an integer.


Three consequences of the definition catch students out:

  • Every nonzero integer divides 00, because 0=a⋅00 = a \cdot 0.
  • Signs do not matter: a∣ba \mid b holds exactly when −a∣b-a \mid b holds, and exactly when a∣−ba \mid -b holds.
  • a∣ba \mid b is a statement that is either true or false, not a number. Do not confuse it with the fraction b/ab/a.

The division algorithm

Divisibility is a special case of a more general fact. The division algorithm states that for any integer aa and any positive integer dd, there is exactly one pair of integers qq (the quotient) and rr (the remainder) such that


a=dq+r,0≤r<d.a = dq + r, \qquad 0 \le r < d.


The quotient is the floor q=⌊a/d⌋q = \lfloor a/d \rfloor, and the remainder is r=a−dqr = a - dq. So d∣ad \mid a exactly when the remainder is 0.


Example: For a=−17a = -17 and d=5d = 5, you get −17/5=−3.4-17/5 = -3.4, so q=⌊−3.4⌋=−4q = \lfloor -3.4 \rfloor = -4 and r=−17−5(−4)=3r = -17 - 5(-4) = 3. Check: 5(−4)+3=−175(-4) + 3 = -17 and 0≤3<50 \le 3 < 5. Writing −17=5(−3)−2-17 = 5(-3) - 2 gives a true equation, but its r=−2r = -2 breaks the condition 0≤r<d0 \le r < d.


A number line from -20 to 0 marks -17 between the multiples -20 and -15, with a bracket above from -20 to -17 and a bracket below from -17 to -15.


Programming languages split at exactly this point. Python's // and % use the floor, so for d>0d > 0 they match the division algorithm. C, C++ and Java truncate the quotient toward zero, so their % result takes the sign of the dividend.


aa

dd

qq

rr

Python a % d

Java a % d

1717

55

33

22

22

22

−17-17

55

−4-4

33

33

−2-2

2020

55

44

00

00

00

−20-20

55

−4-4

00

00

00


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∣ba \mid b and a∣ca \mid c, then a∣(bx+cy)a \mid (bx + cy) for all integers xx and yy.
  • Transitivity: if a∣ba \mid b and b∣cb \mid c, then a∣ca \mid c.

The proof of the first is the template for the rest. Write b=akb = ak and c=alc = al with kk and ll integers. Then bx+cy=a(kx+ly)bx + cy = a(kx + ly), and kx+lykx + ly is an integer, so aa divides the combination. Transitivity works the same way: b=akb = ak and c=bmc = bm give c=a(km)c = a(km).


Example: Since 9∣279 \mid 27 and 9∣459 \mid 45, the property guarantees 9∣(2⋅27−45)9 \mid (2 \cdot 27 - 45), and indeed 54−45=954 - 45 = 9.


The reverse direction does not hold. From a∣(b+c)a \mid (b + c) you cannot conclude a∣ba \mid b: 4∣(1+3)4 \mid (1 + 3), yet 4∤14 \nmid 1. Likewise a∣bca \mid bc does not imply a∣ba \mid b or a∣ca \mid c: 6∣36=4⋅96 \mid 36 = 4 \cdot 9, yet 6 divides neither 4 nor 9. When aa is prime that last implication does hold, a result you meet when you study primes.



What to remember

  • a∣ba \mid b means b=akb = ak for some integer kk; it is a true-or-false statement, not a fraction.
  • For d>0d > 0, every integer aa has exactly one quotient q=⌊a/d⌋q = \lfloor a/d \rfloor and remainder rr with a=dq+ra = dq + r and 0≤r<d0 \le r < d.
  • d∣ad \mid 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>0d > 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 kk goes into bucket rr, the remainder the division algorithm gives for kk with divisor 12, so 0≤r<120 \le r < 12. Some keys are signed sensor offsets, so they can be negative.


a) For the key k=−50k = -50, find the quotient qq and the remainder rr, and state the bucket.


Write −50=12q+r-50 = 12q + r with 0≤r<120 \le r < 12. Dividing gives −50/12≈−4.17-50/12 \approx -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


r=−50−12(−5)=−50+60=10.r = -50 - 12(-5) = -50 + 60 = 10.


Check: 12(−5)+10=−5012(-5) + 10 = -50 and 0≤10<120 \le 10 < 12. The key −50-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-4.17 toward zero gives a quotient of ________. Multiplying back, 12 × (−4) = ________, so Java computes


−50−12(−4)=−50+48=−2.-50 - 12(-4) = -50 + 48 = -2.


So -50 % 12 returns −2-2. That is not a valid bucket number, and using it as an array index throws an exception. Add 12 and reduce again:


((−2)+12) mod 12=10 mod 12=10,\big((-2) + 12\big) \bmod 12 = 10 \bmod 12 = 10,


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=84x = 84 and y=−36y = -36 both land in bucket 0. A rebalancing step stores the combined key 5x−3y5x - 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, 5x5x is ________ and −3y-3y is ________, so


5x−3y=420+108=528=12⋅44.5x - 3y = 420 + 108 = 528 = 12 \cdot 44.


The remainder is 0, so the combined key lands in bucket 0.


For the general case, suppose 12∣x12 \mid x and 12∣y12 \mid y, and write x=12mx = 12m and y=12ny = 12n with mm and nn integers. Then


5x−3y=60m−36n=12(5m−3n),5x - 3y = 60m - 36n = 12(5m - 3n),


and 5m−3n5m - 3n is an integer, so 12∣(5x−3y)12 \mid (5x - 3y). This is the linear-combination property with coefficients 5 and −3. For these keys m=7m = 7 and n=−3n = -3, giving 5m−3n=35+9=445m - 3n = 35 + 9 = 44, which matches the direct calculation.

Complete the definition of divisibility used in discrete mathematics. For integers aa and bb with a≠0a \neq 0, aa divides bb, written a∣ba \mid b, when b=akb = ak for some kk. When this holds, bb is called a of aa.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A program classifies positive integers according to their positive divisors. Complete its test cases by entering prime, composite, or neither for each integer.

IntegerClassification
1
2
27
29

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Apply the division algorithm to the integer a=−83a = -83 with divisor d=9d = 9. Find the quotient qq and the remainder rr that satisfy a=dq+ra = dq + r with 0≤r<d0 \le r < d. Enter each value as an integer.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice