SageMath Lab โ€ข Algebra & Number Theory

Connect Sage to your algebra and number theory courses: integer rings, modular arithmetic, polynomials, primes, and simple sequences. The aim is to make proofs and problem sets feel more experimental and less mysterious.

1. Polynomial rings and factorisation

In Sage you do not just write x^2 - 1 in the air. You build a polynomial ring and name its variable. This keeps the algebra precise and mirrors the way textbooks talk about โ€œpolynomials over โ„šโ€ or โ€œover โ„ค/pโ„คโ€.

๐Ÿ”ค A polynomial ring over the rationals
# Polynomial ring R = QQ[x]
R.<x> = QQ[]       # R is the ring, x is the generator

f = x^4 - 5*x^2 + 4
print("f(x) =", f)

# Factor over the rationals
print("Factorisation over QQ:", factor(f))

# Roots with multiplicities
print("Roots:", f.roots())
Try changing the coefficients and see when the factorisation pattern changes.

A standard move in algebra is to change the base field or ring. Sage lets you try this immediately.

๐Ÿงฎ The same polynomial modulo a prime
# Polynomial ring over a finite field GF(5)
S.<y> = GF(5)[]
g = y^4 - 5*y^2 + 4    # Note: 5 โ‰ก 0 (mod 5), so this simplifies in GF(5)

print("g(y) in GF(5)[y] =", g)
print("Factorisation in GF(5)[y]:", factor(g))
Compare the factorisation over QQ and over GF(5). This mirrors textbook statements about reducibility mod p.

2. Modular arithmetic, inverses, and congruences

Many number theory problems at UI involve congruences, inverses modulo n, and simple questions about divisibility. Sage has first-class support for working in โ„ค/nโ„ค.

๐Ÿ”ข Working in โ„ค/17โ„ค
# The ring of integers modulo 17
R = Integers(17)

a = R(5)
b = R(12)

print("a =", a)
print("b =", b)
print("a + b =", a + b)
print("a * b =", a * b)

# Inverses (if they exist)
print("a inverse:", a^-1)
print("Check:", a * a^-1)
Use this to test examples for homework questions about modular inverses before writing your full solution.
๐Ÿง  Exploring a congruence pattern
# Explore n^2 + n + 1 modulo 7
R = Integers(7)

vals = []
for n in range(0, 14):
    vals.append((n, R(n^2 + n + 1)))

print("n, n^2 + n + 1 (mod 7):")
for n, value in vals:
    print(n, "โ†’", value)
Once you see the pattern numerically, you can try to prove it formally in your written solution.
Idea: when a number theory question feels abstract, use Sage to produce several examples, guess the general statement, and then write a clean proof separating computation from theory.

3. Integer sequences and prime-related functions

Integer sequences are a natural playground: divisibility patterns, parity, growth, behaviour modulo n.

๐Ÿ“œ Building and studying a simple sequence
# A simple sequence: a_n = n^2 + n + 1
def a(n):
    return n^2 + n + 1

values = [a(n) for n in range(1, 21)]
print("First 20 values of a_n:", values)

# Check when a_n is prime
prime_positions = [n for n in range(1, 101) if is_prime(a(n))]
print("n โ‰ค 100 with a_n prime:", prime_positions)
This is a good template for any sequence you meet in a UI course or project.
๐Ÿงฎ Useful built-in prime-related functions
# List primes in an interval
print("Primes between 80 and 120:", list(primes(80, 120)))

# Euler's totient function ฯ†(n)
for n in range(2, 11):
    print("phi(%2d) = %2d" % (n, euler_phi(n)))

# Greatest common divisor and lcm
print("gcd(84, 126) =", gcd(84, 126))
print("lcm(12, 18)  =", lcm(12, 18))
These are often helpful for checking the numerical side of number theory exercises.
Code copied to clipboard