Arithmetic and NumbersMathematics · Book 1
0 / 0

Shelves / Arithmetic and Numbers

AN·05

Factors, Multiples and Primes

Which numbers divide which: factors and multiples, divisibility tests and why each one works (including casting out nines), arithmetic with remainders, primes and why there are infinitely many, the one-and-only prime factorisation of every number, and the greatest common divisor by Euclid's 2,300-year-old algorithm.

38 topics · ~34 min read · 15-question quiz
Updated Suggest a correction

Start here

What this chapter answers

How do you tell whether 918,082 is divisible by 11 without dividing? Why does adding the digits test for 3 and 9, but not for 7? Why is 1 not a prime? How do we know the primes go on forever? Why can every number be broken into primes in only one way, and why does that matter? How do you find the largest number that divides both 1,071 and 462, in three lines? And why does the security of the internet rest on how slowly the method in the lab runs?

Factors in one picture

Take 12 square tiles and arrange them in a rectangle. You can make 1 by 12, 2 by 6 or 3 by 4 (and the same turned round). The side lengths that work, 1, 2, 3, 4, 6 and 12, are the factors of 12. Now take 13 tiles. The only rectangle is 1 by 13, a single row. Numbers like 13, which can only be laid out as one line, are the primes. Every other number can be laid out in a genuine rectangle, and its sides can be split again, until only primes are left.

Factors and multiples

The words

If a=b×c for whole numbers b and c, then b and c are factors (or divisors) of a, and a is a multiple of each. We write b∣a, read "b divides a". So 3∣12 and 5∤12. In the language of chapter 4: b∣a exactly when dividing a by b leaves remainder 0.

Every number is a multiple of 1 and of itself, and every number divides 0 (because 0=b×0). The multiples of 6 are 0, 6, 12, 18, … and never end; the factors of 6 are 1, 2, 3, 6, and always finitely many, since a factor of a≥1 is at most a.

Finding all the factors

Factors come in pairs: if b×c=a, then both b and c are factors. For 36 the pairs are 1 × 36, 2 × 18, 3 × 12, 4 × 9 and 6 × 6. In each pair one factor is at most a (if both were bigger, their product would be bigger than a). So to find every factor, try only up to the square root and write down each partner. For 36 that's trying 1 to 6 instead of 1 to 36.

The pairing also explains a curious fact: a number has an odd number of factors exactly when it is a perfect square, because only then is one factor its own partner (6 × 6). 36 has nine factors; 12 has six.

Useful facts about dividing

  • If d∣a and d∣b, then d∣a+b and d∣a−b. (If a=dm and b=dn, then a+b=d(m+n).)
  • If d∣a, then d∣ka for any whole k.
  • If d∣a and a∣b, then d∣b.

These three facts prove every divisibility test below.

Divisibility tests, and why they work

The last digits: 2, 5 and 10, then 4 and 8

A number is ten times the number formed by all its digits but the last, plus its last digit: 4752=10×475+2. Since 2, 5 and 10 all divide 10, they divide the first part, so whether they divide the number depends only on the last digit:

  • divisible by 2: last digit 0, 2, 4, 6 or 8;
  • by 5: last digit 0 or 5;
  • by 10: last digit 0.

The same argument with 100 instead of 10 gives the tests for 4 and 25 (look at the last two digits, since 4∣100), and with 1,000 the test for 8 (last three digits, since 8∣1000). 7,316 is divisible by 4 because 16 is; 51,000,120 is divisible by 8 because 120 is.

Digit sums: 3 and 9

Every power of ten is one more than a multiple of nine: 10=9+1, 100=99+1, 1000=999+1. So in expanded form,

4752=4×1000+7×100+5×10+2=(4×999+7×99+5×9)+(4+7+5+2).

The first bracket is a multiple of 9 (and so of 3). So a number and its digit sum differ by a multiple of 9, which means:

  • a number is divisible by 9 exactly when its digit sum is;
  • a number is divisible by 3 exactly when its digit sum is (since 3 divides 9).

4,752 has digit sum 18, so it's divisible by 9 (and by 3). The test can be repeated: 18 has digit sum 9.

A number divisible by both 2 and 3 is divisible by 6; by both 3 and 4, by 12. The rule behind that (it needs the two numbers to share no factor) is proved later in this chapter.

The alternating sum: 11

Powers of ten alternate between one more and one less than a multiple of 11: 1=0+1, 10=11−1, 100=99+1, 1000=1001−1, and so on. So a number differs from the alternating sum of its digits, taken from the right (ones minus tens plus hundreds minus thousands …), by a multiple of 11. For 918,082:

2−8+0−8+1−9=−22,

and −22 is a multiple of 11, so 918,082 is divisible by 11 (it's 11×83,462).

And 7?

There's no test for 7 as simple as these, because neither 10 nor its powers are close to multiples of 7 in a regular way: 10 leaves remainder 3, 100 leaves 2, 1,000 leaves 6, and the pattern repeats only every six digits. Tests exist (1001=7×11×13, so a number and its alternating sum of three-digit groups leave the same remainder on division by 7, 11 and 13), but dividing is usually faster. The lab shows how badly the digit-sum test fails for 7.

In other bases

Nothing above is special to ten except the base. In base b, b=(b−1)+1, so the digit sum tests for b−1 and its factors, and the alternating sum tests for b+1. In hexadecimal, the digit sum tests for 15, 5 and 3, and the alternating sum for 17. In binary, the last digit tests for 2, and the alternating sum of bits tests for 3.

Arithmetic with remainders

Remainders add and multiply

Chapter 4's exercises found that if n leaves remainder 3 on division by 7, then 2n leaves 6 and 3n leaves 2. That's a general fact: to find the remainder of a sum or product, you can work with the remainders. Write a=q1m+r1 and b=q2m+r2. Then

a+b=(q1+q2)m+(r1+r2),a×b=(q1q2m+q1r2+q2r1)m+r1r2,

so a+b leaves the same remainder as r1+r2, and a×b the same as r1×r2. Mathematicians write a≡b(modm), "a is congruent to b modulo m", when a and b leave the same remainder on division by m; this is modular arithmetic, or clock arithmetic: on a 12-hour clock, 9 o'clock plus 5 hours is 2 o'clock because 14≡2(mod12).

Why casting out nines works

Chapter 3 checked additions by digit sums and promised a reason. Here it is: a number and its digit sum leave the same remainder on division by 9 (shown above). Remainders add and multiply like the numbers. So if a+b=c, the digit sums of a and b, added, leave the same remainder as the digit sum of c; and the same for products. A mismatch proves an error.

What it can't do is prove an answer right. Any error that changes the answer by a multiple of 9 is invisible to it: two swapped digits (which changes the number by a multiple of 9, since 10a+b−(10b+a) =9(a−b)), a wrong digit off by 9 (0 written for 9), or a whole column shifted when the digits happen to sum the same.

Primes

What a prime is

A prime is a whole number greater than 1 whose only factors are 1 and itself: 2, 3, 5, 7, 11, 13, …. A whole number greater than 1 that isn't prime is composite: it has a factor other than 1 and itself, so it can be written as a product of two smaller numbers.

1 is not prime, by agreement, and for a good reason: if it were, the factorisation theorem below would fail, because 6 =2×3 =1×2×3 =1×1×2×3 would all be different factorisations into primes. Mathematicians in earlier centuries sometimes counted 1 as prime; the modern convention keeps the theorems clean.

2 is the only even prime, since every other even number has 2 as a factor. Every prime after 3 is one more or one less than a multiple of 6 (the others are divisible by 2 or 3), though not every such number is prime (25, 35).

The sieve of Eratosthenes

To list the primes up to 100: write the numbers 2 to 100. Circle 2 and cross out every other multiple of 2. The next uncrossed number, 3, is prime: circle it and cross out its multiples. Then 5, then 7. After 7, stop: any composite number up to 100 has a factor no bigger than 100=10 (the square-root argument again), so it has already been crossed out. Everything still standing is prime: 25 of them below 100.

Interactive diagram: The sieve of Eratosthenes, up to 41. It needs JavaScript; the text around it says the same.

Testing one number

To test whether n is prime, try dividing by the primes up to n. If none divides it, it's prime. Is 221 prime? 221 is a little under 15, so try 2, 3, 5, 7, 11 and 13: 221=13×17. Is 223 prime? None of 2, 3, 5, 7, 11, 13 divides it, so yes.

There is no largest prime

How the primes thin out

Primes get rarer: 25 below 100, 168 below 1,000, 1,229 below 10,000. The prime number theorem, proved in 1896 independently by Jacques Hadamard and Charles-Jean de la Vallée Poussin, says the number of primes up to x is approximately x/lnx, where ln is the natural logarithm: the proportion of primes near x is about 1/lnx. The lab compares the two.

Much about primes is still unknown. Are there infinitely many twin primes, pairs like 11 and 13 or 41 and 43 that differ by 2? Nobody knows; in 2013 Yitang Zhang proved there are infinitely many pairs differing by at most 70 million, and later work lowered that bound to 246. Is every even number greater than 2 the sum of two primes (Goldbach's conjecture, 1742)? It has been checked by computer up to 4×1018 and never proved.

Prime factorisation

Breaking a number into primes

Split a number into two factors, then split those, until only primes remain. A factor tree for 360:

Text
            360
           /   \
         36     10
        /  \    / \
       6    6  2   5
      / \  / \
     2   3 2  3

So 360 =2×2×2×3×3×5 =23×32×5. Starting with a different split (360 = 8 × 45) leads to the same primes. Repeated division by the smallest prime that works gives the same result more systematically: 360 → 180 → 90 → 45 (three 2s), → 15 → 5 (two 3s), → 1 (one 5).

Only one way

Uniqueness gives a fact Euclid proved directly, and which is used constantly: if a prime divides a product, it divides one of the factors. (The prime appears in the product's factorisation, which is the factorisations of the factors put together.) It's false for composite numbers: 6 divides 4×9=36 but divides neither 4 nor 9.

What a factorisation tells you

Once you know n=23×32×5, questions about n become questions about exponents:

  • Its factors are exactly the numbers 2a3b5c with 0≤a≤3, 0≤b≤2, 0≤c≤1. So 360 has (3+1)(2+1)(1+1)=24 factors, without listing them.
  • It's a perfect square exactly when every exponent is even (360 isn't; 3600 =24×32×52 is).
  • One number divides another exactly when each prime's exponent in the first is at most its exponent in the second.

Common factors and common multiples

Greatest common divisor and least common multiple

The greatest common divisor gcd(a,b) is the largest number dividing both a and b; the least common multiple lcm(a,b) is the smallest positive number both divide. With factorisations they're immediate: take each prime to the smaller exponent for the gcd and the larger for the lcm.

84=22×3×7,120=23×3×5;gcd=22×3=12,lcm=23×3×5×7=840.

Since each prime contributes its smaller exponent to one and its larger to the other, gcd(a,b)×lcm(a,b) =a×b: here 12×840 =10080 =84×120.

The gcd simplifies fractions (84120 =710, dividing both by 12; chapter 6). The lcm schedules things: a bus every 12 minutes and a train every 18 minutes, both leaving now, next leave together after lcm(12,18) =36 minutes.

Two numbers with gcd=1 are coprime: they share no prime factor. That's the condition for combining divisibility tests: if a and b are coprime and both divide n, then ab divides n (their primes are all in n's factorisation, and none is shared). So divisible by 3 and 4 means divisible by 12, but divisible by 4 and 6 doesn't mean divisible by 24 (12 is divisible by both).

Euclid's algorithm

Factorising large numbers is slow (the lab shows how slow), but the gcd doesn't need factorising. Euclid's observation: any common divisor of a and b also divides the remainder a−qb, and any common divisor of b and the remainder divides a. So gcd(a,b)=gcd(b,r), and the numbers shrink at every step:

Interactive diagram: Euclid's algorithm: gcd(1071, 462). It needs JavaScript; the text around it says the same.
Text
1071 = 2 × 462 + 147
 462 = 3 × 147 + 21
 147 = 7 × 21  + 0

The last non-zero remainder, 21, is gcd(1071,462). Three divisions, no factorising. The remainders always shrink, so the algorithm always stops, and in fact quickly: the number of steps is never more than about five times the number of digits of the smaller number (Lamé, 1844).

Lab: primes and divisors in code

Experiment 1: the sieve, and how primes thin out

Python
import math

def sieve(n):
    """Sieve of Eratosthenes: cross out the multiples of each prime; what's never crossed out is prime."""
    is_prime = bytearray([1]) * (n + 1)
    is_prime[0] = is_prime[1] = 0
    for p in range(2, math.isqrt(n) + 1):
        if is_prime[p]:
            is_prime[p * p::p] = bytearray(len(range(p * p, n + 1, p)))
    return is_prime

flags = sieve(10**7)
print("primes below 100:", [p for p in range(100) if flags[p]])
print(f"{'x':>10} {'primes up to x':>15} {'x / ln x':>10} {'ratio':>6}")
for k in range(1, 8):
    x = 10**k
    count = sum(flags[: x + 1])
    print(f"{x:>10} {count:>15} {x / math.log(x):>10.0f} {count / (x / math.log(x)):>6.3f}")
Text
primes below 100: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
         x  primes up to x   x / ln x  ratio
        10               4          4  0.921
       100              25         22  1.151
      1000             168        145  1.161
     10000            1229       1086  1.132
    100000            9592       8686  1.104
   1000000           78498      72382  1.084
  10000000          664579     620421  1.071

The sieve starts crossing out at p×p, because smaller multiples of p have a smaller prime factor and are already crossed out, and it stops at n. The table is the prime number theorem at work: x/lnx is always a little low, but the ratio creeps towards 1 as x grows, which is exactly what the theorem claims, and nothing more. (Better approximations exist; the theorem is about the ratio, not the difference.)

Experiment 2: Euclid's algorithm

Python
import random

def euclid(a, b, show=False):
    """gcd(a, b) = gcd(b, a mod b), until the remainder is 0. Returns the gcd and the number of divisions."""
    steps = 0
    while b:
        if show:
            print(f"  {a} = {a // b} × {b} + {a % b}")
        a, b = b, a % b
        steps += 1
    return a, steps

print("gcd:", euclid(1071, 462, show=True))
random.seed(5)
worst = max(euclid(random.randrange(1, 10**6), random.randrange(1, 10**6))[1] for _ in range(100_000))
print("most divisions for 100000 random pairs below a million:", worst)
fib = [1, 1]
while fib[-1] < 10**6:
    fib.append(fib[-1] + fib[-2])
print("consecutive Fibonacci numbers", fib[-3], fib[-2], "need", euclid(fib[-2], fib[-3])[1], "divisions")
Text
  1071 = 2 × 462 + 147
  462 = 3 × 147 + 21
  147 = 7 × 21 + 0
gcd: (21, 3)
most divisions for 100000 random pairs below a million: 23
consecutive Fibonacci numbers 514229 832040 need 28 divisions

The whole algorithm is the line a, b = b, a % b. A hundred thousand random pairs of numbers below a million never needed more than 23 divisions. The slowest inputs of all are consecutive Fibonacci numbers (1, 1, 2, 3, 5, 8, 13, …, each the sum of the two before), where every quotient is 1 and the numbers shrink as slowly as possible; Lamé's bound comes from exactly this. Even then, it's 28 divisions for six-digit numbers.

Experiment 3: testing the tests

Python
def digit_sum(n):
    return sum(int(d) for d in str(n))

def alternating_sum(n):
    """Ones digit minus tens digit plus hundreds digit minus ..."""
    return sum(int(d) * (-1) ** i for i, d in enumerate(reversed(str(n))))

N = range(1, 1_000_001)
print("digit sum test for 9 fails on:", sum(1 for n in N if (n % 9 == 0) != (digit_sum(n) % 9 == 0)), "numbers")
print("digit sum test for 3 fails on:", sum(1 for n in N if (n % 3 == 0) != (digit_sum(n) % 3 == 0)), "numbers")
print("alternating test for 11 fails on:", sum(1 for n in N if (n % 11 == 0) != (alternating_sum(n) % 11 == 0)), "numbers")
print("n and its digit sum leave the same remainder mod 9 for all n:", all(n % 9 == digit_sum(n) % 9 for n in N))
print("digit sum test for 7 fails on:", sum(1 for n in N if (n % 7 == 0) != (digit_sum(n) % 7 == 0)), "numbers")
Text
digit sum test for 9 fails on: 0 numbers
digit sum test for 3 fails on: 0 numbers
alternating test for 11 fails on: 0 numbers
n and its digit sum leave the same remainder mod 9 for all n: True
digit sum test for 7 fails on: 244910 numbers

A million numbers, and the tests for 3, 9 and 11 never fail, as the proofs say they can't. The fourth line is the stronger fact behind casting out nines: not just "divisible or not", but the same remainder every time. The digit sum "test" for 7 is wrong for about a quarter of all numbers: a proof is what separates a test that works from a pattern that happens to fit a few examples.

Experiment 4: factoring is slow

Python
def trial_division(n):
    """Factor n by trying 2, 3, 5, 7, 9, ... up to the square root; count the divisions tried."""
    factors, tries, d = [], 0, 2
    while d * d <= n:
        tries += 1
        while n % d == 0:
            factors.append(d); n //= d
        d += 1 if d == 2 else 2
    if n > 1:
        factors.append(n)
    return factors, tries

for n in (360, 2**31 - 1, 1_000_003 * 1_000_033, 10_000_019 * 10_000_079, 100_000_007 * 100_000_037):
    f, t = trial_division(n)
    print(f"{n:>20} = {' × '.join(map(str, f)):<26} trial divisions: {t:,}")
Text
                 360 = 2 × 2 × 2 × 3 × 3 × 5      trial divisions: 2
          2147483647 = 2147483647                 trial divisions: 23,170
       1000036000099 = 1000003 × 1000033          trial divisions: 500,002
     100000980001501 = 10000019 × 10000079        trial divisions: 5,000,010
   10000004400000259 = 100000007 × 100000037      trial divisions: 50,000,004

360 falls apart almost immediately, because its prime factors are small (and each division shrinks what's left, so the square-root limit drops fast). 231−1 is prime, and proving that by trial division takes 23,170 divisions. For a product of two primes of similar size, trial division must reach the smaller one: every two extra digits in the product multiply the work by ten. RSA uses products of two primes with hundreds of digits each. Far better methods than trial division exist, but every known method on an ordinary computer still slows down so steeply with size that such products haven't been factored, while multiplying the two primes together took a fraction of a second. That gap is what keeps encrypted connections private.

Worked examples

Divisibility without dividing

Using a factorisation

Euclid by hand, and an lcm problem

Exercises

Practice

  1. List all the factors of (a) 48; (b) 100; (c) 97. Which has an odd number of factors, and why?
  2. Use tests to decide which of 2, 3, 4, 5, 8, 9, 11 divide (a) 2,376; (b) 123,456; (c) 1,001.
  3. Use the sieve to list the primes between 100 and 150.
  4. Is 391 prime? Is 397? Which primes did you need to try?
  5. Factorise into primes: (a) 252; (b) 1,001; (c) 4,096; (d) 9,999.
  6. Find gcd and lcm of (a) 36 and 48; (b) 17 and 51; (c) 35 and 64.
  7. Use Euclid's algorithm to find gcd(1001,770).
Answers
  1. (a) 1, 2, 3, 4, 6, 8, 12, 16, 24, 48; (b) 1, 2, 4, 5, 10, 20, 25, 50, 100; (c) 1, 97. Only 100 has an odd number (nine), because it's a perfect square: 10 pairs with itself.
  2. (a) 2,376: 2 (last digit 6), 3 and 9 (digit sum 18), 4 (76), 8 (376 = 8 × 47), 11 (6−7+3−2=0); not 5. (b) 123,456: 2, 3 (digit sum 21, not 9), 4 (56), 8 (456 = 8 × 57); not 5, 9 or 11 (6−5+4−3+2−1=3). (c) 1,001: 11 (1−0+0−1=0); none of the others. (It's 7×11×13.)
  3. 101, 103, 107, 109, 113, 127, 131, 137, 139, 149.
  4. 391=17×23: not prime (try primes up to 19, since 202=400). 397 is prime: none of 2, 3, 5, 7, 11, 13, 17, 19 divides it.
  5. (a) 22×32×7; (b) 7×11×13; (c) 212; (d) 32×11×101.
  6. (a) gcd 12, lcm 144; (b) gcd 17, lcm 51 (17 divides 51); (c) gcd 1 (coprime), lcm 2,240.
  7. 1001=1×770+231; 770=3×231+77; 231=3×77+0. gcd 77.

Harder

  1. Prove that the product of any three consecutive whole numbers is divisible by 6.
  2. A number written in base 10 has digits that are a rearrangement of another number's digits. Prove that their difference is divisible by 9.
  3. Explain why a number with remainder 2 on division by 3 can't be a perfect square. (Work with remainders.)
  4. Find the smallest number that leaves remainder 1 when divided by 2, 3, 4, 5 and 6, and is divisible by 7.
  5. In base 16, why does adding the hex digits test divisibility by 15? Test 4B016.
Answers
  1. Among any three consecutive numbers, at least one is even and exactly one is a multiple of 3. So 2 and 3 both divide the product, and since they're coprime, 6 does.
  2. A number and its digit sum leave the same remainder on division by 9. Rearranging the digits doesn't change the digit sum, so both numbers leave the same remainder, and their difference leaves remainder 0.
  3. Every number leaves remainder 0, 1 or 2 on division by 3, and its square leaves the remainder of 02, 12 or 22=4, that is 0, 1 or 1. So a square never leaves remainder 2.
  4. Remainder 1 on division by 2, 3, 4, 5, 6 means n−1 is a multiple of lcm(2,3,4,5,6) =60: n =61,121,181,241,301,…. The first divisible by 7 is 301=7×43.
  5. In base 16, every power of 16 is one more than a multiple of 15 (16=15+1), so a number and its hex digit sum leave the same remainder on division by 15. 4+11+0=15, so 4B016=1200 is divisible by 15 (1200=15×80).

Challenge

  1. Prove that there are infinitely many primes that leave remainder 3 on division by 4, by adapting Euclid's proof: given a list of such primes, consider N =4×p1×⋯×pk−1. Why does the same trick not work with +1, or for primes of the form 4k+1?
Discussion

N leaves remainder 3 on division by 4, and it's odd. Its prime factors are odd, so each leaves remainder 1 or 3 on division by 4. If all of them left remainder 1, their product would too (remainders multiply: 1×1=1), but N leaves 3. So at least one prime factor leaves remainder 3. None of the pi divides N (it would divide N+1=4p1⋯pk as well, so it would divide 1). That prime is new. With +1, N=4P+1 leaves remainder 1, and a product of primes of the form 4k+3 can leave remainder 1 (3×7=21), so no conclusion follows. For primes of the form 4k+1 the theorem is still true, but the proof needs a different idea (for example, about which numbers can be sums of two squares). Dirichlet proved in 1837 that every such progression a,a+d,a+2d,… with gcd(a,d)=1 contains infinitely many primes.

In real life

Misconceptions and limits

The model to keep

Common misconceptions

Misconception What's actually true Where it's shown
"1 is a prime number." 1 has only one factor; counting it as prime would break the uniqueness of factorisation. What a prime is
"All odd numbers are prime." 9, 15, 21, 25 are odd and composite. 2 is prime and even. What a prime is
"Euclid's number p1⋯pk+1 is always prime." It may not be: 2×3×5×7×11×13+1 =59×509. It just has a new prime factor. There is no largest prime
"If a number is divisible by 4 and by 6, it's divisible by 24." Only coprime tests combine: 12 is divisible by 4 and 6, not by 24. Greatest common divisor and least common multiple
"Digit sums test divisibility by any number." They test 3 and 9 (factors of 10−1) only; for 7 they're wrong about a quarter of the time. Experiment 3: testing the tests
"If casting out nines checks out, the answer is right." It misses any error that's a multiple of 9, including swapped digits. Why casting out nines works
"The gcd and lcm can only be found by factorising." Euclid's algorithm finds the gcd by repeated division, and lcm = ab/gcd. Euclid's algorithm
"Bigger numbers are more likely to be prime." Primes thin out: near x about 1 number in lnx is prime. How the primes thin out

Limits and trade-offs

  • Factorising is hard in general. Small numbers factor at once; products of two large primes resist every known method on ordinary computers. Testing whether a number is prime is, surprisingly, much easier than factoring it (fast primality tests exist), which is what makes RSA keys possible to generate.
  • Some simple questions are open. Twin primes and Goldbach's conjecture are unproved; checking by computer, however far, isn't a proof.
  • Unique factorisation is special. In some number systems that extend the whole numbers it fails; that discovery, in the nineteenth century, created a large part of modern algebra.

Revision sheet

Key ideas

  • b∣a means a=bc; remainder 0. Factors come in pairs; search up to a. Odd number of factors ⇔ perfect square.
  • If d divides a and b, it divides a±b and any multiple of a.
  • Tests: last digit (2, 5, 10), last two (4, 25), last three (8); digit sum (3, 9); alternating sum (11). In base b: b−1 and b+1.
  • Remainders add and multiply like the numbers: a≡b(modm). Casting out nines works because a number and its digit sum are congruent mod 9, and misses multiples of 9.
  • A prime has exactly two factors; 1 is not prime. Sieve up to n; test by primes up to n.
  • Infinitely many primes (Euclid); about x/lnx of them up to x.
  • Fundamental theorem: one factorisation into primes. A prime dividing a product divides a factor.
  • From n=∏pa: number of factors =∏(a+1); square ⇔ all exponents even.
  • gcd: smaller exponents; lcm: larger; gcd×lcm =ab. Coprime: gcd 1.
  • Euclid's algorithm: gcd(a,b) =gcd(b,amodb).

Glossary

  • Factor (divisor): a whole number that divides another with remainder 0.
  • Multiple: the result of multiplying a number by a whole number.
  • Divides (b∣a): a is a multiple of b.
  • Prime number: a whole number greater than 1 whose only factors are 1 and itself.
  • Composite number: a whole number greater than 1 that is not prime.
  • Prime factorisation: a number written as a product of primes, unique apart from order.
  • Sieve of Eratosthenes: a method of listing primes by crossing out the multiples of each prime in turn.
  • Greatest common divisor (gcd): the largest number dividing each of two (or more) numbers.
  • Least common multiple (lcm): the smallest positive number that is a multiple of each of two (or more) numbers.
  • Coprime: having greatest common divisor 1.
  • Euclid's algorithm: finding the gcd by replacing the pair (a,b) with (b,amodb) until the remainder is 0.
  • Congruent modulo m: leaving the same remainder on division by m, written a≡b(modm).
  • Modular arithmetic: arithmetic with remainders, in which numbers wrap around after m, like the hours on a clock.
  • Prime number theorem: the number of primes up to x is approximately x/lnx, in the sense that the ratio tends to 1.