Factors and Multiples
A factor of a number divides it exactly, with no remainder; a multiple of a number is what you get by multiplying it by a whole number. 4 is a factor of 12, and 12 is a multiple of 4. These two words describe the same relationship from opposite ends, and they are the starting point for primes, fractions and the GCF and LCM.
Factors
The factors of 12 are 1, 2, 3, 4, 6 and 12, because each one divides 12 with remainder 0. In Python, " is a factor of " is the test n % d == 0.
Factors come in pairs that multiply to the number:
Arrange 12 counters in a rectangle and these pairs are exactly the possible rectangles: 1 by 12, 2 by 6, 3 by 4.
The pairs give a fast way to find every factor. Test divisors starting from 1, and each time one works, write down its partner too. You can stop once the divisor passes the square root, because after that you only meet partners you already have. For 36, , so you only test 1 to 6:
When the number is a perfect square, the middle pair repeats (), so a square number has an odd count of factors. Every other number has an even count.
import math
def factors(n):
small, large = [], []
for d in range(1, math.isqrt(n) + 1): # isqrt = whole-number square root
if n % d == 0:
small.append(d)
if d != n // d:
large.append(n // d)
return small + large[::-1]
for n in [12, 36, 97, 100]:
f = factors(n)
print(f"{n}: {f} ({len(f)} factors)")12: [1, 2, 3, 4, 6, 12] (6 factors)
36: [1, 2, 3, 4, 6, 9, 12, 18, 36] (9 factors)
97: [1, 97] (2 factors)
100: [1, 2, 4, 5, 10, 20, 25, 50, 100] (9 factors)
97 has only two factors, 1 and itself: it is a prime. You will study primes in Primes and Prime Factorization.
The square-root shortcut matters. To factor a number around one million, the slow method tests a million divisors; this one tests about a thousand.
Facts about factors
- 1 is a factor of every number, and every number is a factor of itself.
- Every factor of (other than ) is at most .
- If is a factor of , then is a factor of every multiple of . (3 divides 12, so 3 divides 24, 36, 120, …)
- If is a factor of both and , it is a factor of and . (3 divides 12 and 21, so it divides 33 and 9.)
Multiples
The multiples of 4 are 4, 8, 12, 16, 20, … : , , , and so on forever. A number has finitely many factors but infinitely many multiples. (0 is also a multiple of every number, , but usually we list the positive ones.)
Python's range with a step lists multiples directly, and n % k == 0 tests whether is a multiple of :
print("multiples of 4:", list(range(4, 41, 4)))
print("multiples of 6:", list(range(6, 61, 6)))
common = [m for m in range(1, 61) if m % 4 == 0 and m % 6 == 0]
print("common multiples of 4 and 6 up to 60:", common)
print("is 1000 a multiple of 8?", 1000 % 8 == 0)multiples of 4: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
multiples of 6: [6, 12, 18, 24, 30, 36, 42, 48, 54, 60]
common multiples of 4 and 6 up to 60: [12, 24, 36, 48, 60]
is 1000 a multiple of 8? True
The smallest common multiple, 12, is the least common multiple, the star of the GCF and LCM lesson.
Perfect numbers: a curiosity
Add up the factors of 6 that are smaller than 6: . A number equal to the sum of its smaller factors is called perfect. They are rare:
def proper_factor_sum(n):
return sum(d for d in range(1, n // 2 + 1) if n % d == 0)
print([n for n in range(2, 10000) if proper_factor_sum(n) == n])[6, 28, 496, 8128]
Only four below 10 000. Nobody knows whether an odd perfect number exists; it is one of the oldest open questions in mathematics.
In software
- Layouts: to arrange 24 thumbnails in a neat rectangle, pick a factor pair: , , . A screen splits evenly into tiles whose size is a common factor of both.
- "Every -th time": a game might autosave every 300 frames or spawn an enemy every 7th wave:
if frame % 300 == 0. That is a test for a multiple. - Memory alignment: processors read data fastest when its address is a multiple of 4, 8 or 16, so compilers pad structures to multiples of these sizes.
- Trial division (testing factors up to ) is the simplest way to test primes, and it shows why large numbers are hard to factor, which cryptography relies on.
Exercises
Exercise 1: List the factors
List all factors of 48 using factor pairs.
Answer
1, 2, 3, 4, 6, 8, 12, 16, 24, 48
Solution
, so test 1 to 6:
Read the small partners up, then the large ones down.
Exercise 2: Factor or multiple?
True or false: (a) 7 is a factor of 91 (b) 45 is a multiple of 6 (c) 1 is a multiple of 1 (d) 16 is a factor of 4
Answer
(a) true (b) false (c) true (d) false
Solution
(a) . (b) remainder 3. (c) . (d) is not a whole number; it is the other way round, 4 is a factor of 16.
Exercise 3: Rectangles of chairs
In how many different ways can 60 chairs be arranged in a rectangle with at least 2 rows and at least 2 chairs per row? (A and a arrangement count as different.)
Answer
10 ways
Solution
60 has 12 factors: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60. The number of rows must be a factor of 60, other than 1 (one row) and 60 (one chair per row). That leaves 10 choices of row count.
rows = [r for r in range(2, 60) if 60 % r == 0]
print(rows, len(rows))[2, 3, 4, 5, 6, 10, 12, 15, 20, 30] 10
Exercise 4: Most factors
Which number from 1 to 100 has the most factors? Use the factors function from this lesson.
Answer
60, 72, 84, 90 and 96 tie with 12 factors each.
Solution
import math
def factors(n):
small, large = [], []
for d in range(1, math.isqrt(n) + 1):
if n % d == 0:
small.append(d)
if d != n // d:
large.append(n // d)
return small + large[::-1]
counts = {n: len(factors(n)) for n in range(1, 101)}
best = max(counts.values())
print(best, [n for n, c in counts.items() if c == best])12 [60, 72, 84, 90, 96]
Numbers built from many small primes () have the most factors, which is why 60 minutes and 360 degrees are so convenient to divide up.
Test yourself
Which of these is NOT a factor of 54?
- 6
- 9
- 12
- 27
To find all factors of 400 with factor pairs, up to which divisor do you need to test?
- 200
- 100
- 40
- 20
Which number has an odd number of factors?
- 18
- 25
- 30
- 50