Number theory is the study of the set of integers . 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 whose only divisors are and itself. The Fundamental Theorem of Arithmetic states that every integer has a unique prime factorization.
Primes are the “atoms” of the number system. We can find them efficiently using the Sieve of Eratosthenes.
Interactive Lab
def sieve(n):
primes = [True] * (n + 1)
p = 2
while (p * p <= n):
if (primes[p]):
for i in range(p * p, n + 1, p):
primes[i] = False
p += 1
return [p for p in range(2, n + 1) if primes[p]]
# Find all primes up to 100
print(f"Primes up to 100: {sieve(100)}")
python
1def sieve(n):
2 primes =[True]*(n +1)
3 p =2
4while(p * p <= n):
5if(primes[p]):
6for i inrange(p * p, n +1, p):
7 primes[i]= False
8 p +=1
9return[p for p inrange(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 and is the largest positive integer that divides both. The Euclidean Algorithm calculates this by repeatedly applying the remainder operation: .
Interactive Lab
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(f"GCD of 48 and 18: {gcd(48, 18)}")
print(f"GCD of 101 and 103: {gcd(101, 103)}")
python
1def gcd(a, b):
2while b:
3 a, b = b, a % b
4return 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 if divides . This system forms a Ring, which is the basis for modern cryptography (like RSA).
Knowledge Check
What is 17 mod 5?
Answer: 2
17 divided by 5 is 3 with a remainder of 2.
What is 17 mod 5?
4. Bezout’s Identity and Modular Inverses
Bezout’s Identity states that for any non-zero , there exist integers such that . 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.
Interactive Lab
def extended_gcd(a, b):
if a == 0:
return b, 0, 1
gcd, x1, y1 = extended_gcd(b % a, a)
x = y1 - (b // a) * x1
y = x1
return gcd, x, y
# Find modular inverse of 3 mod 11
# We need x such that 3x = 1 (mod 11)
g, x, y = extended_gcd(3, 11)
if g == 1:
print(f"Modular inverse of 3 mod 11 is: {x % 11}")
python
1def extended_gcd(a, b):
2if a ==0:
3return b, 0, 1
4 gcd, x1, y1 = extended_gcd(b % a, a)
5 x = y1 -(b // a) * x1
6 y = x1
7return 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:
13print(f"Modular inverse of 3 mod 11 is: {x % 11}")