Search Knowledge

© 2026 LIBREUNI PROJECT

Number Theory

Number Theory: The Queen of Mathematics

Number theory is the study of the set of integers Z\mathbb{Z}. While the objects of study are simple, the problems are among the most difficult and beautiful in all of mathematics.

1. Prime Numbers and Factorization

A prime number is an integer p>1p > 1 whose only divisors are 11 and itself. The Fundamental Theorem of Arithmetic states that every integer n>1n > 1 has a unique prime factorization.

Primes are the “atoms” of the number system. We can find them efficiently using the Sieve of Eratosthenes.

python
1def sieve(n):
2 primes = [True] * (n + 1)
3 p = 2
4 while (p * p <= n):
5 if (primes[p]):
6 for i in range(p * p, n + 1, p):
7 primes[i] = False
8 p += 1
9 return [p for p in range(2, n + 1) if primes[p]]
10 
11# Find all primes up to 100
12print(f"Primes up to 100: {sieve(100)}")

2. Greatest Common Divisor (GCD)

The GCD of two integers aa and bb is the largest positive integer that divides both. The Euclidean Algorithm calculates this by repeatedly applying the remainder operation: gcd(a,b)=gcd(b,amodb)\text{gcd}(a, b) = \text{gcd}(b, a \bmod b).

python
1def gcd(a, b):
2 while b:
3 a, b = b, a % b
4 return a
5 
6print(f"GCD of 48 and 18: {gcd(48, 18)}")
7print(f"GCD of 101 and 103: {gcd(101, 103)}")

3. Modular Arithmetic: “Clock Math”

In modular arithmetic, we work with remainders. We say ab(modn)a \equiv b \pmod{n} if nn divides (ab)(a-b). This system forms a Ring, which is the basis for modern cryptography (like RSA).

What is 17 mod 5?

4. Bezout’s Identity and Modular Inverses

Bezout’s Identity states that for any non-zero a,ba, b, there exist integers x,yx, y such that ax+by=gcd(a,b)ax + by = \text{gcd}(a, b). We can find these coefficients using the Extended Euclidean Algorithm. This is how we find Modular Inverses, which allow us to “divide” in modular arithmetic.

python
1def extended_gcd(a, b):
2 if a == 0:
3 return b, 0, 1
4 gcd, x1, y1 = extended_gcd(b % a, a)
5 x = y1 - (b // a) * x1
6 y = x1
7 return gcd, x, y
8 
9# Find modular inverse of 3 mod 11
10# We need x such that 3x = 1 (mod 11)
11g, x, y = extended_gcd(3, 11)
12if g == 1:
13 print(f"Modular inverse of 3 mod 11 is: {x % 11}")

5. Summary Check

If gcd(a, n) = 1, then a is said to be ___ to n.