Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Find integer solutions to polynomial equations including linear Diophantine equations ax + by = c and Pythagorean triples. Apply number theory to solve Pell's equation, sum of squares problems, and Fermat's Last Theorem cases. SageMath systematically searches for solutions. Jupyter notebook on CoCalc provides algorithmic approaches.

9 views
ubuntu2404
Kernel: SageMath 10.7

Elementary Number Theory with SageMath in CoCalc

Chapter 6: Diophantine Equations

This notebook contains Chapter 6 from the main Elementary Number Theory with SageMath in CoCalc notebook.

For the complete course, please refer to the main notebook: Elementary Number Theory with SageMath in CoCalc.ipynb

Diophantine Equations

Diophantine equations seek integer solutions to polynomial equations. Named after Diophantus of Alexandria (circa 250 CE), these problems bridge number theory and algebra.

Types We'll Explore:

  1. Linear: ax + by = c

  2. Pythagorean triples: x² + y² = z²

  3. Pell's equation: x² - Dy² = 1

Why They Matter:

  • Fundamental to algebraic number theory

  • Applications in cryptography

  • Beautiful mathematical structures

  • Connect to famous conjectures (Fermat's Last Theorem)

# Chapter 6: Linear Diophantine Equations print("LINEAR DIOPHANTINE EQUATIONS\n") print("Form: ax + by = c") print("Goal: Find integer solutions (x, y)") print("Method: Use Extended Euclidean Algorithm") print() def solve_linear_diophantine_complete(a, b, c): """ Completely solve linear Diophantine equation ax + by = c """ print(f"Solving {a}x + {b}y = {c}") print("-" * 40) # Step 1: Check solvability g = gcd(a, b) print(f"Step 1: gcd({a}, {b}) = {g}") if c % g != 0: print(f"No integer solutions: {g} does not divide {c}") return None print(f"Solutions exist: {g} divides {c}") # Step 2: Find particular solution using extended Euclidean algorithm gcd_result, x0, y0 = xgcd(a, b) # Scale to match c scale = c // g x_particular = x0 * scale y_particular = y0 * scale print(f"Step 2: Extended GCD gives {a}×{x0} + {b}×{y0} = {gcd_result}") print(f" Scaling by {scale}: {a}×{x_particular} + {b}×{y_particular} = {c}") # Verify particular solution check = a * x_particular + b * y_particular print(f" Verification: {a}×{x_particular} + {b}×{y_particular} = {check} {'✓' if check == c else '✗'}") # Step 3: General solution b_coeff = b // g a_coeff = a // g print(f"Step 3: General solution (for any integer t):") print(f" x = {x_particular} + {b_coeff}t") print(f" y = {y_particular} - {a_coeff}t") # Step 4: Show specific solutions print(f"Step 4: Specific solutions:") print(f"{'t':>3} | {'x':>6} | {'y':>6} | {'Check':>8}") print("-" * 30) for t in range(-3, 4): x_t = x_particular + b_coeff * t y_t = y_particular - a_coeff * t check_t = a * x_t + b * y_t print(f"{t:3d} | {x_t:6d} | {y_t:6d} | {check_t:8d}") return (x_particular, y_particular, b_coeff, -a_coeff) # Examples with different characteristics examples = [ (3, 5, 1), # Coprime coefficients (6, 9, 15), # Common factor that divides c (12, 18, 30), # Larger common factor (4, 6, 5), # No solution case ] for a, b, c in examples: result = solve_linear_diophantine_complete(a, b, c) print("="*60) print("\nApplications of Linear Diophantine Equations:") print(" • Making change with different coin denominations") print(" • Scheduling problems with time constraints") print(" • Resource allocation in optimization") print(" • Solving systems of congruences") # Practical example: Coin change problem print("\nPRACTICAL EXAMPLE: Coin Change") print("How many ways can we make $1.00 using quarters (25¢) and dimes (10¢)?") print("Equation: 25x + 10y = 100 or simplified: 5x + 2y = 20") result = solve_linear_diophantine_complete(5, 2, 20) if result: x_part, y_part, b_coeff, a_coeff = result print("\nNon-negative solutions (valid coin counts):") valid_solutions = [] for t in range(-10, 11): x = x_part + b_coeff * t y = y_part + a_coeff * t if x >= 0 and y >= 0: valid_solutions.append((x, y)) print(f" {x} quarters + {y} dimes = ${float((25*x + 10*y)/100):.2f}") print(f"\nTotal ways: {len(valid_solutions)}")
LINEAR DIOPHANTINE EQUATIONS Form: ax + by = c Goal: Find integer solutions (x, y) Method: Use Extended Euclidean Algorithm Solving 3x + 5y = 1 ---------------------------------------- Step 1: gcd(3, 5) = 1 Solutions exist: 1 divides 1 Step 2: Extended GCD gives 3×2 + 5×-1 = 1 Scaling by 1: 3×2 + 5×-1 = 1 Verification: 3×2 + 5×-1 = 1 ✓ Step 3: General solution (for any integer t): x = 2 + 5t y = -1 - 3t Step 4: Specific solutions: t | x | y | Check ------------------------------ -3 | -13 | 8 | 1 -2 | -8 | 5 | 1 -1 | -3 | 2 | 1 0 | 2 | -1 | 1 1 | 7 | -4 | 1 2 | 12 | -7 | 1 3 | 17 | -10 | 1 ============================================================ Solving 6x + 9y = 15 ---------------------------------------- Step 1: gcd(6, 9) = 3 Solutions exist: 3 divides 15 Step 2: Extended GCD gives 6×-1 + 9×1 = 3 Scaling by 5: 6×-5 + 9×5 = 15 Verification: 6×-5 + 9×5 = 15 ✓ Step 3: General solution (for any integer t): x = -5 + 3t y = 5 - 2t Step 4: Specific solutions: t | x | y | Check ------------------------------ -3 | -14 | 11 | 15 -2 | -11 | 9 | 15 -1 | -8 | 7 | 15 0 | -5 | 5 | 15 1 | -2 | 3 | 15 2 | 1 | 1 | 15 3 | 4 | -1 | 15 ============================================================ Solving 12x + 18y = 30 ---------------------------------------- Step 1: gcd(12, 18) = 6 Solutions exist: 6 divides 30 Step 2: Extended GCD gives 12×-1 + 18×1 = 6 Scaling by 5: 12×-5 + 18×5 = 30 Verification: 12×-5 + 18×5 = 30 ✓ Step 3: General solution (for any integer t): x = -5 + 3t y = 5 - 2t Step 4: Specific solutions: t | x | y | Check ------------------------------ -3 | -14 | 11 | 30 -2 | -11 | 9 | 30 -1 | -8 | 7 | 30 0 | -5 | 5 | 30 1 | -2 | 3 | 30 2 | 1 | 1 | 30 3 | 4 | -1 | 30 ============================================================ Solving 4x + 6y = 5 ---------------------------------------- Step 1: gcd(4, 6) = 2 No integer solutions: 2 does not divide 5 ============================================================ Applications of Linear Diophantine Equations: • Making change with different coin denominations • Scheduling problems with time constraints • Resource allocation in optimization • Solving systems of congruences PRACTICAL EXAMPLE: Coin Change How many ways can we make $1.00 using quarters (25¢) and dimes (10¢)? Equation: 25x + 10y = 100 or simplified: 5x + 2y = 20 Solving 5x + 2y = 20 ---------------------------------------- Step 1: gcd(5, 2) = 1 Solutions exist: 1 divides 20 Step 2: Extended GCD gives 5×1 + 2×-2 = 1 Scaling by 20: 5×20 + 2×-40 = 20 Verification: 5×20 + 2×-40 = 20 ✓ Step 3: General solution (for any integer t): x = 20 + 2t y = -40 - 5t Step 4: Specific solutions: t | x | y | Check ------------------------------ -3 | 14 | -25 | 20 -2 | 16 | -30 | 20 -1 | 18 | -35 | 20 0 | 20 | -40 | 20 1 | 22 | -45 | 20 2 | 24 | -50 | 20 3 | 26 | -55 | 20 Non-negative solutions (valid coin counts): 0 quarters + 10 dimes = $1.00 2 quarters + 5 dimes = $1.00 4 quarters + 0 dimes = $1.00 Total ways: 3
# Pythagorean Triples print("PYTHAGOREAN TRIPLES\n") print("Finding integer solutions to x² + y² = z²") print("These represent right triangles with integer side lengths!") print() def generate_primitive_pythagorean_triples(limit): """ Generate primitive Pythagorean triples using the parametric method Formula: a = m² - n², b = 2mn, c = m² + n² where m > n > 0, gcd(m,n) = 1, and m,n not both odd """ print("Parametric Generation Method:") print("For m > n > 0 with gcd(m,n) = 1 and m,n not both odd:") print(" a = m² - n²") print(" b = 2mn") print(" c = m² + n²") print() triples = [] print(f"{'m':>2} {'n':>2} | {'a':>3} {'b':>3} {'c':>3} | {'a²+b²':>6} {'c²':>6} | {'Check':>5}") print("-" * 50) m = 2 while m * m + 1 <= limit: # Ensure c ≤ limit for n in range(1, m): # Check conditions for primitive triple if gcd(m, n) == 1 and (m % 2 != n % 2): # Not both odd a = m*m - n*n b = 2*m*n c = m*m + n*n if c <= limit: # Ensure a ≤ b by convention if a > b: a, b = b, a # Verify it's a Pythagorean triple check = a*a + b*b == c*c check_symbol = "✓" if check else "✗" print(f"{m:2d} {n:2d} | {a:3d} {b:3d} {c:3d} | {a*a + b*b:6d} {c*c:6d} | {check_symbol:>5}") if check: triples.append((a, b, c, m, n)) m += 1 return triples # Generate primitive triples limit = 50 primitive_triples = generate_primitive_pythagorean_triples(limit) print(f"\nFound {len(primitive_triples)} primitive Pythagorean triples with c ≤ {limit}") print() # Famous examples print("FAMOUS PYTHAGOREAN TRIPLES:\n") famous_triples = [(3, 4, 5), (5, 12, 13), (8, 15, 17), (7, 24, 25), (20, 21, 29)] print(f"{'Triple':>12} | {'a²':>4} {'b²':>4} {'c²':>4} | {'Sum':>6} | {'Area':>5}") print("-" * 45) for a, b, c in famous_triples: area = (a * b) // 2 # Right triangle area sum_squares = a*a + b*b c_squared = c*c status = "✓" if sum_squares == c_squared else "✗" print(f"({a:2d},{b:2d},{c:2d}) | {a*a:4d} {b*b:4d} {c*c:4d} | {sum_squares:6d} | {area:5d}") print() # All Pythagorean triples from primitive ones print("ALL PYTHAGOREAN TRIPLES:\n") print("Every Pythagorean triple is either primitive or a multiple of a primitive triple.") # Show multiples of the first primitive triple if primitive_triples: a, b, c, m, n = primitive_triples[0] print(f"\nMultiples of primitive triple ({a}, {b}, {c}):") for k in range(1, 5): ka, kb, kc = k*a, k*b, k*c print(f" k={k}: ({ka:2d}, {kb:2d}, {kc:2d})") print("\n🏛️ Historical Note: Pythagorean triples were known to Babylonians around 1800 BCE!") print(" Tablet Plimpton 322 lists many examples, predating Pythagoras by over 1000 years.") # Connection to rational points on unit circle print("\nGEOMETRIC INTERPRETATION:") print("Pythagorean triples (a,b,c) correspond to rational points on the unit circle:") print(" (x,y) = (a/c, b/c) satisfies x² + y² = 1") if primitive_triples: a, b, c, m, n = primitive_triples[0] x_coord = Rational(a, c) y_coord = Rational(b, c) print(f"\nExample: ({a}, {b}, {c}) → ({x_coord}, {y_coord}) on unit circle") print(f"Check: ({x_coord})² + ({y_coord})² = {x_coord**2 + y_coord**2} = 1 ✓")
PYTHAGOREAN TRIPLES Finding integer solutions to x² + y² = z² These represent right triangles with integer side lengths! Parametric Generation Method: For m > n > 0 with gcd(m,n) = 1 and m,n not both odd: a = m² - n² b = 2mn c = m² + n² m n | a b c | a²+b² c² | Check -------------------------------------------------- 2 1 | 3 4 5 | 25 25 | ✓ 3 2 | 5 12 13 | 169 169 | ✓ 4 1 | 8 15 17 | 289 289 | ✓ 4 3 | 7 24 25 | 625 625 | ✓ 5 2 | 20 21 29 | 841 841 | ✓ 5 4 | 9 40 41 | 1681 1681 | ✓ 6 1 | 12 35 37 | 1369 1369 | ✓ Found 7 primitive Pythagorean triples with c ≤ 50 FAMOUS PYTHAGOREAN TRIPLES: Triple | a² b² c² | Sum | Area --------------------------------------------- ( 3, 4, 5) | 9 16 25 | 25 | 6 ( 5,12,13) | 25 144 169 | 169 | 30 ( 8,15,17) | 64 225 289 | 289 | 60 ( 7,24,25) | 49 576 625 | 625 | 84 (20,21,29) | 400 441 841 | 841 | 210 ALL PYTHAGOREAN TRIPLES: Every Pythagorean triple is either primitive or a multiple of a primitive triple. Multiples of primitive triple (3, 4, 5): k=1: ( 3, 4, 5) k=2: ( 6, 8, 10) k=3: ( 9, 12, 15) k=4: (12, 16, 20) 🏛️ Historical Note: Pythagorean triples were known to Babylonians around 1800 BCE! Tablet Plimpton 322 lists many examples, predating Pythagoras by over 1000 years. GEOMETRIC INTERPRETATION: Pythagorean triples (a,b,c) correspond to rational points on the unit circle: (x,y) = (a/c, b/c) satisfies x² + y² = 1 Example: (3, 4, 5) → (3, 4) on unit circle Check: (3)² + (4)² = 25 = 1 ✓

Practice Problems

Test your understanding of elementary number theory:

# Practice Problems print("🎯 PRACTICE PROBLEMS\n") # Problem 1: Prime factorization and divisors print("Problem 1: Prime Factorization Analysis") print("Find the prime factorization of 2520 and count its divisors.") print() n = 2520 factorization = factor(n) print(f"Solution:") print(f" 2520 = {factorization}") # Count divisors using formula: d(n) = ∏(αᵢ + 1) where n = ∏pᵢ^αᵢ divisor_count = 1 for prime, exp in factorization: # CHANGED divisor_count *= (exp + 1) actual_divisors = divisors(n) print(f" Number of divisors: d(2520) = {divisor_count}") print(f" Verification: {len(actual_divisors)} divisors found ✓") print(f" First 10 divisors: {actual_divisors[:10]}") print() # Problem 2: Extended Euclidean Algorithm print("Problem 2: Bézout's Identity") print("Find integers x, y such that 252x + 198y = gcd(252, 198)") print() a, b = 252, 198 g, x, y = xgcd(a, b) print(f"Solution:") print(f" gcd(252, 198) = {g}") print(f" 252 × {x} + 198 × {y} = {g}") print(f" Verification: 252 × {x} + 198 × {y} = {252*x + 198*y} ✓") print() # Problem 3: Chinese Remainder Theorem print("Problem 3: System of Congruences") print("Solve the system:") print(" x ≡ 2 (mod 5)") print(" x ≡ 3 (mod 7)") print(" x ≡ 4 (mod 11)") print() remainders = [2, 3, 4] moduli = [5, 7, 11] solution = crt(remainders, moduli) M = 5 * 7 * 11 print(f"Solution:") print(f" x ≡ {solution} (mod {M})") print(f" Verification:") for r, m in zip(remainders, moduli): check = solution % m status = "✓" if check == r else "✗" print(f" {solution}{check}{r} (mod {m}) {status}") print() # Problem 4: Euler's totient function print("Problem 4: Totient Function") print("Compute φ(720) using the prime factorization method.") print() n = 720 factorization = factor(n) phi_n = euler_phi(n) print(f"Solution:") print(f" 720 = {factorization}") # Apply φ(n) = n × ∏(1 - 1/p) result = n calculation_steps = [f"φ(720) = 720"] for prime, exp in factorization: # CHANGED result = result * (prime - 1) // prime calculation_steps.append(f" × (1 - 1/{prime})") formula = "".join(calculation_steps) print(f" {formula} = {phi_n}") print(f" This means {phi_n} integers from 1 to 720 are coprime to 720.") print() # Problem 5: Linear Diophantine equation print("Problem 5: Diophantine Equation") print("Find all integer solutions to 15x + 25y = 5") print() a, b, c = 15, 25, 5 g = gcd(a, b) print(f"Solution:") print(f" gcd(15, 25) = {g}") print(f" Since {g} divides {c}, solutions exist.") # Find particular solution gcd_result, x0, y0 = xgcd(a, b) scale = c // g x_part = x0 * scale y_part = y0 * scale print(f" Particular solution: x = {x_part}, y = {y_part}") print(f" General solution: x = {x_part} + {b//g}t, y = {y_part} - {a//g}t") print(f" Check: 15×{x_part} + 25×{y_part} = {15*x_part + 25*y_part} = 5 ✓") print("\nChallenge Problems:") print("1. Find the next primitive Pythagorean triple after (20, 21, 29)") print("2. Prove that φ(n) is even for all n > 2") print("3. Show that if gcd(a,n) = 1, then a^φ(n) ≡ 1 (mod n) (Euler's theorem)") print("4. Find the smallest positive integer that leaves remainder 1 when divided by") print(" 2, 3, 4, 5, and 6") print("5. Characterize all integers n such that φ(n) = n/2")
🎯 PRACTICE PROBLEMS Problem 1: Prime Factorization Analysis Find the prime factorization of 2520 and count its divisors. Solution: 2520 = 2^3 * 3^2 * 5 * 7 Number of divisors: d(2520) = 48 Verification: 48 divisors found ✓ First 10 divisors: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Problem 2: Bézout's Identity Find integers x, y such that 252x + 198y = gcd(252, 198) Solution: gcd(252, 198) = 18 252 × 4 + 198 × -5 = 18 Verification: 252 × 4 + 198 × -5 = 18 ✓ Problem 3: System of Congruences Solve the system: x ≡ 2 (mod 5) x ≡ 3 (mod 7) x ≡ 4 (mod 11) Solution: x ≡ 367 (mod 385) Verification: 367 ≡ 2 ≡ 2 (mod 5) ✓ 367 ≡ 3 ≡ 3 (mod 7) ✓ 367 ≡ 4 ≡ 4 (mod 11) ✓ Problem 4: Totient Function Compute φ(720) using the prime factorization method. Solution: 720 = 2^4 * 3^2 * 5 φ(720) = 720 × (1 - 1/2) × (1 - 1/3) × (1 - 1/5) = 192 This means 192 integers from 1 to 720 are coprime to 720. Problem 5: Diophantine Equation Find all integer solutions to 15x + 25y = 5 Solution: gcd(15, 25) = 5 Since 5 divides 5, solutions exist. Particular solution: x = 2, y = -1 General solution: x = 2 + 5t, y = -1 - 3t Check: 15×2 + 25×-1 = 5 = 5 ✓ Challenge Problems: 1. Find the next primitive Pythagorean triple after (20, 21, 29) 2. Prove that φ(n) is even for all n > 2 3. Show that if gcd(a,n) = 1, then a^φ(n) ≡ 1 (mod n) (Euler's theorem) 4. Find the smallest positive integer that leaves remainder 1 when divided by 2, 3, 4, 5, and 6 5. Characterize all integers n such that φ(n) = n/2

CoCalc Platform Features for Number Theory

This notebook leverages CoCalc's powerful features:

Number Theory Specific Benefits:

  • SageMath integration: Built-in number theory functions

  • Symbolic computation: Exact arithmetic with large integers

  • Collaborative research: Share proofs and computations

  • LaTeX support: Beautiful mathematical typesetting

Advanced Features:

  1. Large integer arithmetic: Handle RSA-sized numbers

  2. Primality testing: Miller-Rabin and other advanced tests

  3. Factorization algorithms: Pollard rho, quadratic sieve

  4. Elliptic curves: Modern cryptographic applications

Key Theorems and Results:

  • Fundamental Theorem of Arithmetic: Unique prime factorization

  • Euclidean Algorithm: Ancient but efficient GCD method

  • Fermat's Little Theorem: a^(p-1) ≡ 1 (mod p) for prime p

  • Chinese Remainder Theorem: Solving congruence systems

  • Euler's Theorem: Generalization of Fermat's Little Theorem

Noteworthy Connections:

  • Number theory connects to cryptography (RSA, elliptic curves)

  • Geometry appears in Pythagorean triples and rational points

  • Abstract algebra emerges from modular arithmetic

  • Analysis enters through prime distribution and L-functions

Applications:

  • Computer Security: RSA encryption, digital signatures

  • Error Correction: Coding theory and data integrity

  • Algorithm Design: Hashing, pseudorandom generation

  • Pure Mathematics: Foundation for advanced number theory

The Journey Continues:

Elementary number theory is just the beginning! Advanced topics await:

  • Quadratic forms and reciprocity laws

  • Algebraic integers and class field theory

  • L-functions and the Riemann hypothesis

  • Elliptic curves and modern arithmetic geometry

"Mathematics is the queen of sciences, and number theory is the queen of mathematics." - Carl Friedrich Gauss

You've now explored the royal court of mathematics!