This book uses Python, one of the most widely used programming languages in science and engineering. You will need three things.
Python itself. Download and install Python from python.org. Choose the latest stable release. During installation on Windows, check the box that says “Add Python to PATH” — this lets you run Python from the terminal without extra steps.
Three libraries. Open a terminal (Command Prompt on Windows, Terminal on macOS or Linux) and type:
pip install sympy mpmath matplotlib
SymPy does exact symbolic mathematics: factoring integers, simplifying expressions, solving equations, working with exact fractions and square roots. It never rounds.
mpmath handles high-precision arithmetic. With mpmath you can compute pi to 10,000 decimal places or check whether two enormous numbers are truly equal.
matplotlib draws graphs, scatter plots, spiral diagrams, and every other visualization in this book.
Each library is introduced at the point in the book where it is first needed. SymPy appears as early as Chapter 1. mpmath waits until Chapter 10 (Pi and Mathematical Constants). matplotlib is used whenever a picture is worth more than a table.
A way to run the code. The code in this book is written for Quarto, a publishing system that weaves Python code and prose into a single document. Install Quarto from quarto.org. The VS Code editor, with the Quarto extension, lets you run each code block individually and see the output right below it — which is the best way to experiment as you read.
If you prefer, every code block can also be pasted directly into a Jupyter notebook or typed into the Python shell (python at the terminal). The output will be the same.
How to Run the Code in This Book
Every code block in this book looks like this:
# Last digits of squares: do they form a pattern?for n inrange(1, 16): square = n * n last_digit = square %10print(n, square, " last digit:", last_digit)
1 1 last digit: 1
2 4 last digit: 4
3 9 last digit: 9
4 16 last digit: 6
5 25 last digit: 5
6 36 last digit: 6
7 49 last digit: 9
8 64 last digit: 4
9 81 last digit: 1
10 100 last digit: 0
11 121 last digit: 1
12 144 last digit: 4
13 169 last digit: 9
14 196 last digit: 6
15 225 last digit: 5
Run this and look at the last-digit column. Only six values appear: 0, 1, 4, 5, 6, and 9. No square ever ends in 2, 3, 7, or 8. That is a fact about all integers, not just the first fifteen — and you have just discovered it experimentally. We will explain why in Chapter 2 (2 Modular Arithmetic and Check Digits), when we study the arithmetic of remainders.
This is the pattern of the book. The code is short. The output is concrete. The question “why?” leads somewhere interesting.
Throughout the book, every output shown was produced by running the code exactly as written. There are no numbers typed in by hand. If you change a value and re-run the block, you are already doing experimental mathematics.
A note on output width. Some code blocks use pprint (pretty-print) or textwrap.fill to wrap long output to 60 characters per line. This keeps the text from spilling off the edge of a printed page. When you see those, the result is simply long — nothing unusual is happening.
A note on frozen output. This book is published with freeze: auto, meaning code is executed once and the output is stored. If you render the book yourself, each block will re-run and produce fresh output. The stored and live outputs should be identical; if they differ, a library version has changed.
How This Book Is Organized
The chapters are arranged roughly in order of mathematical complexity, so ideas introduced early are available — and referenced back — in later chapters.
Chapters 1-2: Foundations. Chapter 1 (1 Prime Numbers: The Atoms of Arithmetic) explores prime numbers — the atoms of arithmetic — and introduces the Python tools used throughout the book. Chapter 2 (2 Modular Arithmetic and Check Digits) covers modular arithmetic, the mathematics of remainders, and shows how it underpins everything from clock arithmetic to credit card check digits.
Chapter 3: Cryptography and Steganography. Chapter 3 (3 Cryptography and Steganography: The Art of Hidden Messages) shows how modular arithmetic locks and unlocks secrets — from the ancient Caesar cipher to the RSA algorithm that secures today’s internet — and reveals how hidden messages can be embedded inside ordinary-looking images.
Chapter 4: Number Bases. Writing a number in a different base (4 Number Bases) can hide or reveal structure invisible in base 10: digital roots become modular arithmetic mod \((b-1)\) (2 Modular Arithmetic and Check Digits), repunit primes generalize Mersenne primes (1 Prime Numbers: The Atoms of Arithmetic), and the Carl Sagan base-11 hook from the Introduction finally gets its full explanation.
Chapters 8-10: Deeper Number Theory. Continued fractions (Chapter 8, 8 Continued Fractions: Fractions of Fractions) give the best rational approximations to irrational numbers. The OEIS (Chapter 9, 9 Integer Sequences and the OEIS) is the world’s largest catalogue of integer sequences and a genuine research tool. Chapter 10 (10 \pi and Mathematical Constants) investigates pi and other constants, including a formula that extracts any single hexadecimal digit of pi without computing the digits before it.
Chapters 15-16: Caution and Communication. Chapter 15 (15 Computational Misleads: When Computers Lie) catalogs ways computers mislead us: floating-point errors, patterns that eventually fail, and the Strong Law of Small Numbers. Chapter 16 (16 Communicating Mathematics) explains how to communicate what you find — how to write a conjecture, document computational evidence, and present results to others.
Each chapter ends with a collection of Further Research Topics, listed in order from accessible to ambitious. These range from exercises you can finish in an afternoon to questions that could anchor a semester-long independent study. Coming up with a good research question is one of the hardest parts of doing research. The lists at the end of each chapter take that burden off your shoulders — leaving you free to focus on the research itself.
You do not need calculus. You need curiosity, and a computer.
# First things first {#sec-preface .unnumbered}## Setting Up Python and SymPy {#sec-intro-setup}This book uses Python, one of the most widely used programming languagesin science and engineering. You will need three things.**Python itself.** Download and install Python from[python.org](https://python.org). Choose the latest stable release.During installation on Windows, check the box that says "Add Python toPATH" --- this lets you run Python from the terminal without extra steps.**Three libraries.** Open a terminal (Command Prompt on Windows, Terminalon macOS or Linux) and type:```pip install sympy mpmath matplotlib```- **SymPy** does exact symbolic mathematics: factoring integers, simplifying expressions, solving equations, working with exact fractions and square roots. It never rounds.- **mpmath** handles high-precision arithmetic. With mpmath you can compute pi to 10,000 decimal places or check whether two enormous numbers are truly equal.- **matplotlib** draws graphs, scatter plots, spiral diagrams, and every other visualization in this book.Each library is introduced at the point in the book where it is firstneeded. SymPy appears as early as Chapter 1. mpmath waits untilChapter 10 (Pi and Mathematical Constants). matplotlib is used whenevera picture is worth more than a table.**A way to run the code.** The code in this book is written for[Quarto](https://quarto.org), a publishing system that weaves Pythoncode and prose into a single document. Install Quarto from quarto.org.The VS Code editor, with the Quarto extension, lets you run each codeblock individually and see the output right below it --- which is thebest way to experiment as you read.If you prefer, every code block can also be pasted directly into aJupyter notebook or typed into the Python shell (`python` at theterminal). The output will be the same.## How to Run the Code in This Book {#sec-intro-run}Every code block in this book looks like this:```{python}# Last digits of squares: do they form a pattern?for n inrange(1, 16): square = n * n last_digit = square %10print(n, square, " last digit:", last_digit)```Run this and look at the last-digit column. Only six values appear: 0,1, 4, 5, 6, and 9. No square ever ends in 2, 3, 7, or 8. That is afact about all integers, not just the first fifteen --- and you have justdiscovered it experimentally. We will explain why in Chapter 2(@sec-modular), when we study the arithmetic of remainders.This is the pattern of the book. The code is short. The output isconcrete. The question "why?" leads somewhere interesting.Throughout the book, every output shown was produced by running the codeexactly as written. There are no numbers typed in by hand. If you changea value and re-run the block, you are already doing experimentalmathematics.**A note on output width.** Some code blocks use `pprint` (pretty-print)or `textwrap.fill` to wrap long output to 60 characters per line. Thiskeeps the text from spilling off the edge of a printed page. When yousee those, the result is simply long --- nothing unusual is happening.**A note on frozen output.** This book is published with `freeze: auto`,meaning code is executed once and the output is stored. If you renderthe book yourself, each block will re-run and produce fresh output. Thestored and live outputs should be identical; if they differ, a libraryversion has changed.## How This Book Is Organized {#sec-intro-org}The chapters are arranged roughly in order of mathematical complexity,so ideas introduced early are available --- and referenced back --- inlater chapters.**Chapters 1-2: Foundations.**Chapter 1 (@sec-primes) explores prime numbers --- the atoms ofarithmetic --- and introduces the Python tools used throughout the book.Chapter 2 (@sec-modular) covers modular arithmetic, the mathematics ofremainders, and shows how it underpins everything from clock arithmeticto credit card check digits.**Chapter 3: Cryptography and Steganography.**Chapter 3 (@sec-crypto) shows how modular arithmetic locks and unlockssecrets --- from the ancient Caesar cipher to the RSA algorithm thatsecures today's internet --- and reveals how hidden messages can beembedded inside ordinary-looking images.**Chapter 4: Number Bases.**Writing a number in a different base (@sec-bases) can hide or revealstructure invisible in base 10: digital roots become modular arithmeticmod $(b-1)$ (@sec-modular), repunit primes generalize Mersenne primes(@sec-primes), and the Carl Sagan base-11 hook from the Introductionfinally gets its full explanation.**Chapters 5-7: Famous Sequences and Structures.**The Collatz conjecture (Chapter 5, @sec-collatz) shows how a rule achild can state has resisted every proof for nearly a century. Fibonaccinumbers (Chapter 6, @sec-fib) connect rabbits, spirals, the goldenratio, and matrix exponentiation. Pascal's triangle (Chapter 7,@sec-pascal) hides fractals inside ordinary binomial coefficients.**Chapters 8-10: Deeper Number Theory.**Continued fractions (Chapter 8, @sec-cfrac) give the best rationalapproximations to irrational numbers. The OEIS (Chapter 9, @sec-oeis)is the world's largest catalogue of integer sequences and a genuineresearch tool. Chapter 10 (@sec-constants) investigates pi and otherconstants, including a formula that extracts any single hexadecimaldigit of pi without computing the digits before it.**Chapters 11-14: Emergence and Randomness.**Cellular automata (Chapter 11, @sec-automata) show how simple localrules produce global complexity. The logistic map (Chapter 12,@sec-logistic) exhibits chaos from a single quadratic equation.Fractals (Chapter 13, @sec-fractals) measure infinite detail with onenumber. Random walks (Chapter 14, @sec-walks) model diffusion, stockprices, and the wandering path of a drunkard.**Chapters 15-16: Caution and Communication.**Chapter 15 (@sec-misleads) catalogs ways computers mislead us:floating-point errors, patterns that eventually fail, and the StrongLaw of Small Numbers. Chapter 16 (@sec-comm) explains how tocommunicate what you find --- how to write a conjecture, documentcomputational evidence, and present results to others.Each chapter ends with a collection of **Further Research Topics**,listed in order from accessible to ambitious. These range fromexercises you can finish in an afternoon to questions that could anchora semester-long independent study. Coming up with a good researchquestion is one of the hardest parts of doing research. The lists atthe end of each chapter take that burden off your shoulders --- leavingyou free to focus on the research itself.You do not need calculus. You need curiosity, and a computer.