Path: blob/main/foundations/01-modular-arithmetic-groups/sage/01a-integers-and-division.ipynb
483 views
Integers and Division
Module 01a | Modular Arithmetic and Groups
The division algorithm hides a powerful structure that underlies all of modern cryptography.
Question: You need to send the number 42 to your friend, but your channel can only transmit values 0 through 6. How do you encode it? And how does your friend recover the original?
By the end of this notebook, you'll see that the answer, remainders, is the seed of an entire number system.
Objectives
By the end of this notebook you will be able to:
State the division algorithm and explain what makes it useful
Use
divmod()andMod()in SageMath for exact modular arithmeticRecognize remainder patterns as the foundation of modular arithmetic
Prerequisites
This is the first notebook in Module 01. You need:
Basic familiarity with Python syntax
A working SageMath installation (or access to CoCalc/SageMathCell)
No prior knowledge of abstract algebra is assumed.
The Division Algorithm
Every integer division produces a quotient and remainder:
This isn't just arithmetic, it's a theorem: for any integers and , there exist unique and . Let's see it in action.
What if we only cared about the remainder? Watch what happens when we compute remainders of different numbers mod 7:
Remainder Patterns
Checkpoint: Before running the next cell, predict: if you compute
k mod 7for k = 0, 1, 2, ..., 20, how many distinct values will you see?
The remainders cycle through {0, 1, 2, 3, 4, 5, 6}, exactly 7 distinct values, repeating forever. This isn't a coincidence: the division algorithm guarantees , so there are exactly 7 possible remainders. These 7 "bins" are the building blocks of .
SageMath's Mod(): More Than Just %
Python's % operator returns an ordinary integer. SageMath's Mod() creates something richer, an element that lives in the world of remainders.
Common mistake: "
Mod(37, 7)is just a fancy way to write37 % 7 = 2." No!Mod(37, 7)creates a number that lives in . When you add two such numbers, the result automatically stays in . This distinction, between "computing a remainder" and "working inside a number system", becomes critical in the next notebook.
Exercises
Exercise 1 (Worked)
Compute divmod(1234, 17). Verify that . Then create Mod(1234, 17) and confirm the remainder matches.
Exercise 2 (Guided)
Build a remainder table: for each modulus , print k mod n for .
Question: For which does every remainder from 0 to appear equally often among ? Why doesn't it always work out evenly?
Exercise 3 (Independent)
Without running code first, predict:
If and , what is ?
What is ?
Verify with SageMath. Then test: does this "compute the remainders separately, then combine" trick work for any modulus? Try with some examples.
Summary
| Concept | Key idea |
|---|---|
| Division algorithm | with , and the pair is unique |
| Remainder cycling | Remainders mod cycle through exactly values: |
Mod(a, n) | Creates an element that lives in , not just a leftover integer |
| Wrap-around arithmetic | Arithmetic on Mod elements automatically stays inside the remainder system |
The division algorithm looks like basic arithmetic, but the "remainder world" it creates is the foundation of everything that follows.
Crypto teaser: Every number in RSA lives in this remainder world. The modulus is the product of two secret primes, and the security of RSA depends on the difficulty of factoring back into those primes.
Next: Modular Arithmetic, we turn remainders into a complete number system.