Number Theory Basics
Number theory studies integers and their divisibility structure. In discrete mathematics it supplies proof examples, efficient algorithms, and the arithmetic foundation for cryptography. The central objects are divisibility, primes, greatest common divisors, and integer representations.
The topic is both ancient and computational. Divisibility statements train proof writing; Euclid's algorithm is one of the oldest efficient algorithms; prime factorization leads directly to modern public-key cryptography. The habit to build here is to move between equations, divisibility notation, and algorithms without losing track of hypotheses such as "nonzero" or "positive."
Definitions
For integers and with , divides , written , if there is an integer such that . If no such integer exists, write . Divisibility is a statement about exact integer multiples, not approximate division.
The division algorithm says that for every integer and positive integer , there are unique integers and such that
Here is the quotient and is the remainder. This theorem is the basis for modular arithmetic and for Euclid's algorithm.
An integer is prime if its only positive divisors are and . An integer greater than that is not prime is composite. The number is neither prime nor composite.
The greatest common divisor of integers and , not both zero, is the largest positive integer dividing both. It is written . The least common multiple of nonzero integers and is the smallest positive integer divisible by both, written .
Integers and are relatively prime if . A base- expansion of a positive integer is an expression
where each digit satisfies and .
Key results
Basic divisibility facts:
for all integers . Proof: if and , then , and is an integer.
The fundamental theorem of arithmetic states that every integer greater than can be written uniquely as a product of primes, up to the order of the factors. This is why prime factorization can be used to compute gcds, lcms, and divisibility.
If is composite, then has a prime divisor at most . Proof: write with and . If both and were greater than , then , impossible. So one factor is at most , and that factor has a prime divisor at most itself.
Euclid's algorithm rests on
If , then a common divisor of and also divides , and a common divisor of and also divides . Thus the common divisors are the same.
There are infinitely many primes. Suppose there were only finitely many, . Let
No listed prime divides , because division by any leaves remainder . But , so by prime factorization it has a prime divisor. That prime is not on the list, contradiction.
For positive integers ,
This follows by comparing prime exponents: the gcd takes minimum exponents, the lcm takes maximum exponents, and the sum of the minimum and maximum is the sum of the two original exponents.
Visual
| Task | Main tool | Key bound or fact |
|---|---|---|
| test divisibility | definition | quotient must be integer |
| find quotient and remainder | division algorithm | |
| test primality by trial division | prime divisor bound | test primes |
| compute gcd | Euclidean algorithm | repeated remainders decrease |
| compute lcm | gcd-lcm identity | for positive |
| convert base | repeated division | remainders become digits |
Worked example 1: Use the Euclidean algorithm and back-substitution
Problem. Compute and express it as an integer combination of and .
Method.
- Apply the Euclidean algorithm:
- The last nonzero remainder is , so .
- Back-substitute:
- Substitute :
- Substitute :
Checked answer. , and . The combination confirms that any common divisor of and must divide .
Worked example 2: Test primality and factor a number
Problem. Determine whether is prime. If it is composite, factor it.
Method.
- Compute the square-root cutoff:
- Test primes at most : .
- is odd, so .
- Sum of digits is , so .
- It does not end in or , so .
- Check : , so .
- Check : , so .
- Check : .
Checked answer. is composite, and . The search could stop as soon as the factor was found. If no prime up to had divided it, then would have been prime.
Code
def gcd(a, b):
a, b = abs(a), abs(b)
while b:
a, b = b, a % b
return a
def factor(n):
result = []
d = 2
while d * d <= n:
while n % d == 0:
result.append(d)
n //= d
d += 1 if d == 2 else 2
if n > 1:
result.append(n)
return result
def to_base(n, base):
if n == 0:
return [0]
digits = []
while n:
n, r = divmod(n, base)
digits.append(r)
return digits[::-1]
print(gcd(252, 198))
print(factor(221))
print(to_base(241, 2))
The factor function is trial division. It is suitable for small examples and for learning the logic, but large-scale factoring requires much more sophisticated algorithms.
Common pitfalls
- Calling prime. It is neither prime nor composite; otherwise unique factorization would fail.
- Forgetting the condition in the definition of .
- Using the division algorithm with a negative divisor. The standard statement takes the divisor to be positive.
- Stopping trial division too late or too early. Testing primes up to is enough; testing only to is unnecessary.
- Confusing gcd with lcm in prime factorizations. Gcd uses minimum exponents; lcm uses maximum exponents.
- Treating Euclid's algorithm as a black box without preserving the back-substitution equations needed for Bezout coefficients.
Divisibility proofs should always return to the definition. To prove , exhibit an integer with . To disprove it, show no such integer can exist, often by using remainders, parity, or bounds. Manipulating the vertical bar as if it were an arithmetic fraction is a common source of invalid reasoning.
The division algorithm is also the reason remainders are well-defined. For a positive divisor , every integer falls into exactly one of the remainder classes . Negative dividends still have nonnegative remainders in the standard convention: for example, , so the remainder on division by is .
Prime factorization gives a second way to compute gcd and lcm. If
then
This method is conceptually clear but requires factoring, which may be expensive for large integers. Euclid's algorithm avoids full factorization and is much more efficient for gcd computation.
Base expansion is another application of repeated division. Dividing by the base gives the least significant digit as a remainder; repeating on the quotient gives the next digit. The digits appear in reverse order of discovery, which is why base-conversion algorithms usually collect remainders and then reverse the list.
When doing Euclidean algorithm work by hand, keep the quotient-remainder equations aligned and do not overwrite them. The same equations used to find the gcd are needed for back-substitution. Losing one equation often forces the computation to be repeated from the beginning.
For primality testing, test divisibility only by primes up to the square root, not every integer. If a composite number has a factor larger than , the matching factor is smaller than . This is the reason trial division can stop at the square-root boundary. It is also the reason finding no small prime divisor proves primality.
When using prime factorizations, keep the same prime list for both numbers by allowing exponent . For example, and . Then minimum and maximum exponents can be read componentwise for gcd and lcm without forgetting primes that appear in only one number.
Connections
- Proof techniques explains the direct and contradiction proofs used for divisibility and primes.
- Modular arithmetic and cryptography builds congruences and RSA from gcds and primes.
- Algorithms and complexity analyzes Euclid's algorithm, trial division, and base conversion.
- Induction and recursion proves statements about integer representations and recursive algorithms.