TExcelTAD
Home › Basic Math & Pre-Algebra › Primes and Prime Factorization

Primes and Prime Factorization

A prime number is a whole number greater than 1 whose only factors are 1 and itself: 2, 3, 5, 7, 11, 13, … Every other whole number above 1 can be built by multiplying primes together, in exactly one way. That makes primes the atoms of arithmetic, and it makes them the foundation of the encryption that protects every website you visit.

Prime or composite?

Kind Definition Examples
prime exactly two factors: 1 and itself 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
composite more than two factors 4=2×24 = 2 \times 2, 9=3×39 = 3 \times 3, 15=3×515 = 3 \times 5
neither 0 and 1

Notes on the edges:

  • 1 is not prime. It has only one factor. Excluding it keeps the "exactly one way" rule below true (otherwise 6=2×3=1×2×3=1×1×2×3…6 = 2 \times 3 = 1 \times 2 \times 3 = 1 \times 1 \times 2 \times 3 \ldots).
  • 2 is the only even prime. Every other even number has 2 as a factor.

To test whether nn is prime, try dividing by 2,3,4,…2, 3, 4, \ldots up to n\sqrt{n}. If none divides it, nn is prime. As in Factors and Multiples, any factor above n\sqrt{n} would pair with one below it, which you would already have found. Is 221 prime? 221≈14.9\sqrt{221} \approx 14.9; testing 2, 3, 5, 7, 11, 13 finds 221=13×17221 = 13 \times 17. So no.

python · try it yourself
import math

def is_prime(n):
    if n < 2:
        return False
    for d in range(2, math.isqrt(n) + 1):
        if n % d == 0:
            return False
    return True

for n in [1, 2, 9, 97, 221, 1009]:
    print(n, is_prime(n))
1 False
2 True
9 False
97 True
221 False
1009 True

The sieve of Eratosthenes

To find all primes up to some limit, an ancient method beats testing each number. Write the numbers 2 to NN. Circle 2 and cross out all its multiples. The next uncrossed number (3) is prime: cross out its multiples. Repeat. Whatever survives is prime.

python · try it yourself
def sieve(limit):
    is_p = [True] * (limit + 1)
    is_p[0] = is_p[1] = False
    for p in range(2, int(limit ** 0.5) + 1):
        if is_p[p]:
            for multiple in range(p * p, limit + 1, p):
                is_p[multiple] = False
    return [n for n in range(limit + 1) if is_p[n]]

primes = sieve(100)
print(primes)
print(len(primes), "primes below 100;", len(sieve(10000)), "below 10 000")
[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]
25 primes below 100; 1229 below 10 000

Crossing out starts at p×pp \times p because smaller multiples of pp (such as 2p2p, 3p3p) were already crossed out by smaller primes.

Primes thin out as numbers grow (25 of the first 100 numbers, about 12% of the first 10 000) but they never stop. Euclid's proof is short: suppose there were a last prime. Multiply all the primes together and add 1. That new number leaves remainder 1 when divided by every prime on the list, so its prime factors are not on the list. The list was not complete after all.

Prime factorization

The Fundamental Theorem of Arithmetic says every whole number above 1 is a product of primes in exactly one way (ignoring the order). Finding it is called prime factorization. A factor tree splits the number until only primes are left:

text
          360
         /   \
       36     10
      /  \    / \
     6    6  2   5
    / \  / \
   2  3 2   3
360=2×2×2×3×3×5=23×32×5360 = 2 \times 2 \times 2 \times 3 \times 3 \times 5 = 2^3 \times 3^2 \times 5

Any split works; the tree may look different, but the primes at the bottom are always the same. Writing repeated primes with exponents (232^3 means 2×2×22 \times 2 \times 2) keeps it short (see Exponents).

In code, divide by the smallest prime as often as it goes, then move to the next divisor:

python · try it yourself
def factorize(n):
    result = {}
    d = 2
    while d * d <= n:
        while n % d == 0:
            result[d] = result.get(d, 0) + 1
            n //= d
        d += 1
    if n > 1:                 # whatever is left is a prime
        result[n] = result.get(n, 0) + 1
    return result

def show(f):
    return " x ".join(f"{p}^{e}" if e > 1 else str(p) for p, e in f.items())

for n in [360, 97, 1001, 1024, 123456]:
    print(n, "=", show(factorize(n)))
360 = 2^3 x 3^2 x 5
97 = 97
1001 = 7 x 11 x 13
1024 = 2^10
123456 = 2^6 x 3 x 643

SymPy does the same with sympy.factorint(360), which returns {2: 3, 3: 2, 5: 1}.

The factorization tells you everything about a number's factors. 360=23×32×5360 = 2^3 \times 3^2 \times 5 has (3+1)×(2+1)×(1+1)=24(3 + 1) \times (2 + 1) \times (1 + 1) = 24 factors, because each factor picks 0–3 twos, 0–2 threes and 0–1 fives. It also makes GCF and LCM easy, and it is how fractions are simplified.

In software

  • Cryptography: RSA encryption multiplies two huge primes (hundreds of digits each). Multiplying is instant; factoring the product back is so slow that nobody can do it in practice. That one-way difficulty keeps the private key secret (see Asymmetric Encryption).
  • Hash tables often use a prime number of buckets, so keys with patterns (all multiples of 4, say) still spread evenly.
  • Game design and animation: layering cycles with prime lengths (7, 11, 13 frames) makes repeating patterns line up rarely, so motion looks less mechanical.
  • Interview classics: "is this prime?" and "sieve up to nn" are standard warm-up problems.

Exercises

Exercise 1: Prime or not?

Decide which are prime: 51, 53, 87, 91, 101.

Answer

53 and 101 are prime; 51, 87 and 91 are not.

Solution
51=3×17(digit sum 6, divisible by 3)53: 53≈7.3: not divisible by 2,3,5,7→prime87=3×29(digit sum 15)91=7×13(the classic trap)101: 101≈10.05: not divisible by 2,3,5,7→prime\begin{aligned} &51 = 3 \times 17 && \text{(digit sum 6, divisible by 3)} \\ &53{:}\ \sqrt{53} \approx 7.3{:}\ \text{not divisible by } 2, 3, 5, 7 && \to \text{prime} \\ &87 = 3 \times 29 && \text{(digit sum 15)} \\ &91 = 7 \times 13 && \text{(the classic trap)} \\ &101{:}\ \sqrt{101} \approx 10.05{:}\ \text{not divisible by } 2, 3, 5, 7 && \to \text{prime} \end{aligned}

Exercise 2: Factor tree

Find the prime factorization of 1 200.

Answer

24×3×522^4 \times 3 \times 5^2

Solution
1 200=12×100=(2×2×3)×(2×2×5×5)=24×3×52check: 16×3×25=48×25=1 200 ✓\begin{aligned} &1\,200 = 12 \times 100 = (2 \times 2 \times 3) \times (2 \times 2 \times 5 \times 5) = 2^4 \times 3 \times 5^2 \\ &\text{check: } 16 \times 3 \times 25 = 48 \times 25 = 1\,200\ \checkmark \end{aligned}

Exercise 3: Count the factors

How many factors does 1 200 have? Use its factorization, then check with code.

Answer

30

Solution

With 24×31×522^4 \times 3^1 \times 5^2, a factor picks 0–4 twos, 0–1 threes and 0–2 fives: (4+1)×(1+1)×(2+1)=5×2×3=30(4 + 1) \times (1 + 1) \times (2 + 1) = 5 \times 2 \times 3 = 30.

python · try it yourself
print(sum(1 for d in range(1, 1201) if 1200 % d == 0))
30

Exercise 4: Twin primes

Twin primes are pairs of primes that differ by 2, like (11, 13). Use a sieve to list all twin-prime pairs below 100.

Answer

(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73): 8 pairs.

Solution

Sieve once, then check each prime pp for whether p+2p + 2 is also prime.

python · try it yourself
limit = 100
is_p = [True] * (limit + 1)
is_p[0] = is_p[1] = False
for p in range(2, 11):
    if is_p[p]:
        for m in range(p * p, limit + 1, p):
            is_p[m] = False

twins = [(p, p + 2) for p in range(2, limit - 1) if is_p[p] and is_p[p + 2]]
print(twins, len(twins))
[(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)] 8

Whether there are infinitely many twin primes is still unproven.

Test yourself

Which of these is prime?

  1. 59
  2. 1
  3. 57
  4. 91

What is the prime factorization of 84?

  1. 4×214 \times 21
  2. 2×422 \times 42
  3. 22×3×72^2 \times 3 \times 7
  4. 2×3×142 \times 3 \times 14

To test whether 527 is prime, which divisors must you try?

  1. Every number up to 527
  2. Every number up to 263
  3. Primes up to 22 (since 527≈22.96\sqrt{527} \approx 22.96)
  4. Only 2, 3 and 5