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โคโ.
# 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.
# 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โค.
# 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.
# 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.
# 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.