First things first

Setting Up Python and SymPy

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 in range(1, 16):
    square = n * n
    last_digit = square % 10
    print(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 5-7: Famous Sequences and Structures. The Collatz conjecture (Chapter 5, 5  The Collatz Conjecture: Simple Rules, Deep Mystery) shows how a rule a child can state has resisted every proof for nearly a century. Fibonacci numbers (Chapter 6, 6  Fibonacci Numbers and Their Cousins) connect rabbits, spirals, the golden ratio, and matrix exponentiation. Pascal’s triangle (Chapter 7, 7  Pascal’s Triangle: Arithmetic in a Grid) hides fractals inside ordinary binomial coefficients.

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 11-14: Emergence and Randomness. Cellular automata (Chapter 11, 11  Cellular Automata: Order from Simple Rules) show how simple local rules produce global complexity. The logistic map (Chapter 12, 12  The Logistic Map: Chaos from a Simple Equation) exhibits chaos from a single quadratic equation. Fractals (Chapter 13, 13  Fractals: Infinite Detail at Every Scale) measure infinite detail with one number. Random walks (Chapter 14, 14  Random Walks: Wandering with Purpose) model diffusion, stock prices, and the wandering path of a drunkard.

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.