Abakcus
← Back to home

Abakcus — Book Lists

The 23 Best Math Books for Programmers, in Reading Order

The mathematics a working programmer actually uses, from counting and logic to algorithms, cryptography and the bits inside the machine. 23 books in five steps, four of them free.

Ten mathematics books for programmers laid out in two rows, among them Concrete Mathematics, Introduction to Algorithms, The Algorithm Design Manual and Hacker's Delight

Tap a cover to open the book.

23

books, in the order to read them

5

steps, from a first way in to the bits inside the machine

61

years between the oldest book (1961) and the newest (2022)

4

of them free to read online, legally

You can write software for years without proving a theorem. Then one day a loop that was fast on a thousand records takes all night on a million, or two different users are handed the same identifier, or a sum of money is off by a cent, and the explanation turns out to be mathematics. The word algorithm itself is a mathematician's name. It is how medieval Latin wrote al-Khwarizmi, the scholar of ninth-century Baghdad whose books taught the world to calculate by fixed rules.

The mathematics behind programming is mostly not the mathematics of school. It has little to do with trigonometry or long calculations. It is about counting possibilities, reasoning with logic, graphs of things joined to other things, chance, and whole numbers and their remainders. This is called discrete mathematics, and most programmers who feel that they are bad at maths have simply never been shown it.

The 23 books here are in five steps. The first is ways in, for readers who have code but little mathematics. The second is discrete mathematics, the core of the subject. The third is algorithms and how to measure them. The fourth covers what computers can and cannot do, along with randomness, information and secrets. The last comes back to the machine itself, to bits, rounding errors and three-dimensional graphics.

Four of the books are free from their authors and I have marked them. Two neighbouring subjects have pages of their own: linear algebra, which is the language of graphics and machine learning, and probability and statistics. And if you have never written a proof, keep a book on proofs beside the second step.

In a hurry

Which book should a programmer start with?

Before the books

Four questions to try first

  • Question 1 · A count

    How many times does the loop run?

    A program compares every item in a list of 100 with every item that comes after it. How many comparisons does it make? And how many for a list of 1,000?

    Show the answer

    The first item is compared with 99 others, the second with 98, and so on down to 1. The sum 99 + 98 + … + 1 is 99 × 100 ÷ 2 = 4,950. For 1,000 items it is 999 × 1,000 ÷ 2 = 499,500. Ten times the data means about a hundred times the work, which is why code like this feels fine in testing and fails in production.

    The art of sums in Concrete Mathematics
  • Question 2 · A search

    Twenty questions

    A sorted list holds a million names. You may look at one entry at a time and see whether your name comes before or after it. How many looks do you need, at most, to find a name?

    Show the answer

    Twenty. Look at the middle entry, and half the list is ruled out. Each look halves what is left, and halving a million twenty times brings it below one, because 2²⁰ = 1,048,576. For a billion names you would need only thirty. This is binary search, and the gap between a million steps and twenty is what the study of algorithms is about.

    How to think this way, in Skiena
  • Question 3 · A coincidence

    Twenty-three people, 365 days

    You give each new user a random number from 1 to 365 as an identifier. How many users can you add before it is more likely than not that two of them share a number?

    Show the answer

    Only 23. The chance that 23 random choices from 365 are all different is a little under one half. This is the famous birthday problem, and it surprises almost everyone. Its lesson for programmers is that random identifiers start to collide when their count reaches about the square root of the number of possible values, far sooner than intuition says.

    Hashing and chance in Mitzenmacher and Upfal
  • Question 4 · A bit trick

    A power of two in one line

    For a positive whole number x, the expression x & (x − 1) takes the bitwise AND of x and the number just below it. What does it do to x, and what does it tell you when the result is zero?

    Show the answer

    It switches off the lowest 1 bit of x. Take 12, which is 1100 in binary. One less is 1011, and the AND of the two is 1000, which is 8. If the result is zero, x had only a single 1 bit to begin with, so x is a power of two: 16 is 10000, 15 is 01111, and they share no bits. One operation replaces a loop.

    Hundreds more in Hacker's Delight

Step I · Books 01–04

Find a way in

Four books for readers who have the code but not the mathematics. One is a story, one is a tour, and two teach by having you program.

No. 01 · Step I

Algorithms to Live By: The Computer Science of Human Decisions

Brian Christian and Tom Griffiths · 2016

This is the most enjoyable book on the list and the only one with no exercises. Brian Christian is a writer and Tom Griffiths a cognitive scientist, and their idea is that the problems computer scientists have solved are the same problems people face every day. When should you stop looking at apartments and take one? How do you choose between a restaurant you love and one you have never tried? In what order should you do the things on your list?

Each chapter takes one such question and explains the mathematics that answers it: optimal stopping, the trade-off between exploring and exploiting, sorting, caching, scheduling, reasoning from small samples, and the danger of fitting a model too closely to the past. There are no formulas to work through, but the ideas are stated correctly, and a programmer will finish it understanding why these subjects were worth inventing.

Best forAnyone who wants to see what algorithmic thinking is before studying it.

No. 02 · Step I

Good Math: A Geek's Guide to the Beauty of Numbers, Logic, and Computation

Mark C. Chu-Carroll · The Pragmatic Bookshelf · 2013

Mark Chu-Carroll is a software engineer with a doctorate in computer science who for years wrote a popular blog explaining mathematics to other programmers. This book collects the best of that writing into short chapters, each of which can be read in one sitting.

It starts with numbers and asks what they really are: how the whole numbers can be built from almost nothing, why some numbers cannot be written as fractions, and what is special about a few famous constants. Then it moves to logic, with small programs you can run, to sets and the different sizes of infinity, and finally to computation, where it explains finite-state machines, Turing machines and the lambda calculus that functional languages are built on. It does not go deep into any one subject. It shows you the territory and lets you decide where to go next.

Best forProgrammers who are curious about mathematics but were never taught the interesting parts.

No. 03 · Step I

Math for Programmers: 3D Graphics, Machine Learning, and Simulations with Python

Paul Orland · Manning · 2020

Paul Orland's book starts from the assumption that you can already program in Python and uses that skill to teach you mathematics. Every idea is introduced, turned into code, and put to work on something you can see on the screen.

The first part is about vectors and matrices, and you learn them by drawing and rotating three-dimensional shapes. The second is calculus, taught by simulating moving objects, from a simple asteroid game to fluid flow and sound. The third applies both to machine learning: fitting a line to data, classifying, and finally building and training a small neural network from scratch. There are hundreds of short exercises and several longer projects. It covers continuous mathematics, the kind used in graphics, physics and data, and it is the gentlest serious introduction to it that I know for someone who thinks in code.

Best forWorking programmers who want to learn the mathematics by writing code.

No. 04 · Step I

Essential Math for Data Science

Thomas Nield · O'Reilly · 2022

Thomas Nield's book is short, practical and honest about its purpose. It is for programmers who have started using data science libraries and realised that they do not understand what the functions are doing. In about three hundred pages it goes through the mathematics those libraries rest on.

There is a brisk review of basic mathematics and calculus, then probability, descriptive and inferential statistics, linear algebra, linear and logistic regression, and a first neural network. Each topic is explained in plain language and then carried out in a few lines of Python, so you can change the numbers and see what happens. Nield also gives sensible advice about what a data job actually involves. The book does not prove things, and it will not replace a proper course in statistics. As a first map of what you need to learn, it is very good.

Best forDevelopers moving into data work who need the fundamentals quickly.

Step II · Books 05–08

Discrete mathematics

Counting, logic, proofs, graphs and sums. This is the mathematics that computer science is written in, and one of these four books is the most useful thing on the page.

No. 05 · Step II · Free edition

Discrete Mathematics: An Open Introduction

Oscar Levin · third edition 2019

Oscar Levin teaches at the University of Northern Colorado, and he wrote this textbook for his own students and then gave it to everyone. It is the best free starting point for discrete mathematics, and it is better than many books that cost a great deal.

It covers four things well: counting, sequences, logic and proofs, and graph theory. Every section opens with a puzzle to investigate before you are told anything, so you meet each idea as a question first. The explanations are unhurried and the exercises are plentiful, many with hints and solutions. The online edition is interactive, with answers you can reveal one step at a time, and it is always the most recent version. The printed copy is inexpensive. If you have never studied this subject, start here.

Best forBeginners and self-learners who want a friendly, free first textbook.

No. 06 · Step II

Discrete Mathematics: Elementary and Beyond

László Lovász, József Pelikán and Katalin Vesztergombi · Springer · 2003

László Lovász is one of the great mathematicians of our time, an Abel Prize winner whose work joins combinatorics to computer science, and this small book began as notes for a course he gave to undergraduates. It treats discrete mathematics as a living subject full of beautiful problems.

The chapters are short and each is built around questions. How many ways can a group be seated? Why do the Fibonacci numbers turn up when you count? How do you match people to jobs, colour a map, or find the cheapest network joining a set of towns? Along the way you learn induction, binomial coefficients, a little probability and number theory, and a good deal about graphs, and the last chapters reach cryptography and error-correcting codes. The proofs are real but presented as discoveries. It is lighter and more charming than a standard thousand-page textbook, and it teaches more.

Best forReaders who want to see why mathematicians love this subject.

No. 07 · Step II · Free edition

Mathematics for Computer Science

Eric Lehman, F. Thomson Leighton and Albert R. Meyer · MIT · revised regularly

This is the textbook of the mathematics course that computer science students take at MIT, written by three people who have taught it. It is large, around a thousand pages, and the authors release it freely, so it has become the standard free reference for the subject.

It has five parts. The first teaches how to write proofs, including induction in several forms. The second covers the structures of computer science: number theory with its use in encryption, graphs, relations and networks. The third is counting, the fourth probability, and the fifth recurrences, the equations that describe how long recursive programs take. The examples come from real computing throughout, and the tone is informal and often funny. It moves faster and goes much further than Levin, so it makes an excellent second book, and video lectures of the course are free online as well.

Best forStudents who want the whole first-year MIT course, at no cost.

No. 08 · Step II

Concrete Mathematics: A Foundation for Computer Science

Ronald L. Graham, Donald E. Knuth and Oren Patashnik · Addison-Wesley · 1989, second edition 1994

Donald Knuth began teaching a course at Stanford in 1970 to give students the mathematics his own books required, and this is the book that came out of it. The title is a blend of continuous and discrete, and it is also a joke at the expense of abstract mathematics. Everything in it is a technique for getting an exact answer.

The subjects are sums and how to evaluate them, recurrences, the floor and ceiling functions, elementary number theory, binomial coefficients, special sequences of numbers, generating functions, discrete probability and estimates for large values. The writing is witty, and the margins carry remarks scribbled by the students who first took the course. There are more than five hundred exercises, graded from warm-ups to research problems, and every one has an answer in the back. It is a hard book and you must work through it with a pencil. Programmers who do so speak of it with affection for the rest of their lives.

Best forReaders who want to become truly good at the calculations behind algorithm analysis.

Step III · Books 09–13

Algorithms and their analysis

Here the mathematics is put to work. These five books teach how to design a method that solves a problem and how to say, before running it, how long it will take.

No. 09 · Step III

The Algorithm Design Manual

Steven S. Skiena · Springer · 1997, third edition 2020

Steven Skiena of Stony Brook University wrote the algorithms book that practising programmers find most useful, because it is about the step that other books skip: how to recognise which known problem your new problem really is.

The first half teaches the techniques, among them sorting, searching, graph algorithms, dynamic programming and what to do when a problem is too hard to solve exactly. It is lighter on proofs than a university text and heavier on judgement. Scattered through it are what Skiena calls war stories, accounts of real consulting jobs in which the first idea failed and a better one had to be found. The second half is a catalogue of the classic algorithmic problems, each with a picture, a description of when it arises, advice on how to attack it and pointers to existing software. People keep the book for that catalogue for decades.

Best forWorking programmers, and anyone preparing for technical interviews.

No. 10 · Step III

Introduction to Algorithms

Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein · MIT Press · 1990, fourth edition 2022

Known everywhere by its authors' initials, CLRS is the most widely used algorithms textbook in the world and the one other books cite. It is enormous, well over a thousand pages, and it is rigorous. Every algorithm is given in careful pseudocode, proved correct and analysed for its running time.

It begins with the mathematical tools, such as growth of functions, recurrences and probabilistic analysis, and then works through sorting, data structures, dynamic programming, greedy methods, graph algorithms and a long series of advanced topics, from string matching to linear programming and the theory of hard problems. The fourth edition adds chapters on newer subjects, including online algorithms and machine learning, and prints its figures in colour. Nobody reads it from cover to cover. You study the chapters your course assigns and then return to it for the rest of your working life.

Best forStudents taking a university course, and everyone who needs the standard reference.

No. 11 · Step III · Free edition

Algorithms

Jeff Erickson · 2019

Jeff Erickson has taught algorithms at the University of Illinois for more than twenty years, and his lecture notes were famous among students long before he published them as a book. He gives it away on his website and sells the printed copy at close to cost.

Its great strength is recursion. Erickson teaches you to solve a problem by assuming that a smaller version of it has already been solved, and he shows patiently how backtracking, dynamic programming and greedy methods all grow out of that one habit of thought. The second half is about graphs: searching them, shortest paths, spanning trees, flows and cuts, and finally the problems for which no fast method is known. The prose is clear and often funny, and the exercises are excellent and numerous. It covers less than CLRS and explains what it covers better.

Best forStudents who want a free textbook that teaches how to invent algorithms.

No. 12 · Step III

An Introduction to the Analysis of Algorithms

Robert Sedgewick and Philippe Flajolet · Addison-Wesley · 1996, second edition 2013

Most algorithm books describe running time with a rough upper bound. Robert Sedgewick of Princeton and the French mathematician Philippe Flajolet teach something sharper: how to work out the average number of steps a program takes, accurately enough to predict its performance and compare it with a rival.

The first half of the book is the mathematics needed, namely recurrences, generating functions and estimates for large inputs. The second half applies it to the basic objects of computing, trees, permutations, strings and hash tables, and derives how the standard algorithms on them behave. It is the natural continuation of Concrete Mathematics, more specialised and somewhat more demanding, and it is the best preparation for Knuth's volumes, the next book here. The authors' online course follows the book chapter by chapter.

Best forReaders who want to predict how a program will perform, precisely.

No. 13 · Step III

The Art of Computer Programming, Volume 1: Fundamental Algorithms

Donald E. Knuth · Addison-Wesley · 1968, third edition 1997

Donald Knuth set out in the 1960s to write one book about how to program computers. It became the work of his life, a series of volumes still in progress, and it created the analysis of algorithms as a subject. This first volume is where it starts.

The first half is called Mathematical Preliminaries, and it is a course in itself on induction, sums, number theory, permutations, binomial coefficients and generating functions. The second half covers the basic ways of organising data in a machine: lists, stacks, queues, trees and the management of memory. Everything is treated with complete precision, with history in the notes and exercises rated on a scale from trivial to unsolved research problems. The programs are written for an imaginary machine in its own assembly language, which takes patience. Few people finish it, but an evening with any twenty pages will show you the standard to which the subject was built.

Best forAnyone who wants to read the founding work of the field.

Step IV · Books 14–19

Computation, chance and secrets

What can be computed at all, and what cannot? How does randomness make programs faster? What is information, and how is it kept secret? Six books on the deep questions.

No. 14 · Step IV

Introduction to the Theory of Computation

Michael Sipser · Cengage · 1996, third edition 2012

Michael Sipser has taught the theory of computation at MIT for decades, and his textbook is admired above all for one habit. Before every proof he gives a paragraph called the proof idea, which tells you in plain words why the result is true. Only then does he write out the details.

The book has three parts. The first is about simple models of machines and the patterns they can recognise, which is the theory behind regular expressions and parsers. The second is about what no program can ever do, beginning with the proof that no program can decide in general whether another program will halt. The third is about problems that can be solved but perhaps not quickly, and it leads to the most famous open question in computer science, whether P equals NP. It is short for a textbook, and beautifully organised.

Best forStudents who want the clearest account of what computers can and cannot do.

No. 15 · Step IV

The Annotated Turing

Charles Petzold · Wiley · 2008

In 1936 Alan Turing, then twenty-four, published a paper on a question in mathematical logic. To answer it he had to say exactly what it means to calculate, and the imaginary device he described for the purpose is what we now call a Turing machine. Every computer built since is a version of it.

The paper is hard to read on its own. Charles Petzold, known for his books on programming, reprints the whole of it and surrounds each passage with explanation. He supplies the background in logic and number theory, works through Turing's machines one state at a time, points out the small mistakes in the original, and follows the consequences into later decades. You read every sentence Turing wrote and understand it. It is the best way I know to meet a founding document of any science at first hand.

Best forProgrammers who want to read the paper that started computer science.

No. 16 · Step IV

The Nature of Computation

Cristopher Moore and Stephan Mertens · Oxford University Press · 2011

Cristopher Moore and Stephan Mertens are physicists who work on computation, and they wrote a book about the theory of computing that reads as if the authors could not wait to tell you each result. It is nearly a thousand pages long, and it is one of the few textbooks people read for pleasure.

Its centre is the question of why some problems are easy and others seem impossibly hard, and it explains P and NP with more examples and more insight than any other book. From there it ranges over the limits of computation, memory and time, the power of randomness, interactive proofs, counting and sampling, the sudden transitions at which random problems turn from easy to hard, and quantum computing. The authors explain the idea behind every proof before giving it, and they treat puzzles and games as seriously as theorems. It asks for mathematical maturity but little prior knowledge.

Best forReaders who want the big ideas of computer science told with real enthusiasm.

No. 17 · Step IV

Probability and Computing

Michael Mitzenmacher and Eli Upfal · Cambridge University Press · 2005, second edition 2017

A surprising amount of modern software works by tossing coins. Hash tables, load balancers, the filters that test whether an item has been seen before, and the sampling methods of machine learning all rely on chance, and they work because the probability of failure can be proved to be tiny. This is the standard textbook on how such proofs are made.

Michael Mitzenmacher of Harvard and Eli Upfal of Brown begin with basic probability and build up the tools one at a time: expectation, the inequalities that bound how far a random quantity strays from its average, the mathematics of throwing balls into bins, random graphs, Markov chains and random walks. Every tool is introduced through an algorithm that uses it. The second edition adds material on sampling and on learning from data. It expects that you have met probability before. If you have not, start with a first book on probability.

Best forProgrammers who want to understand hashing, sampling and randomised algorithms.

No. 18 · Step IV

An Introduction to Information Theory: Symbols, Signals and Noise

John R. Pierce · 1961, second edition 1980

In 1948 Claude Shannon, an engineer at Bell Laboratories, published a theory that defined information as something that can be measured, and gave its unit a name, the bit. He showed how far any message can be compressed and how fast it can be sent reliably through a noisy channel. All of digital communication rests on those results.

John Pierce was Shannon's colleague at Bell Labs and a distinguished engineer himself, and he wrote this account for general readers while the theory was still new. He explains entropy, the coding of messages, noise and the capacity of a channel with a minimum of formulas and a great many examples, and then looks at what the theory says about language, physics, psychology and art. It remains the best non-technical introduction to the subject, and the Dover edition costs very little. Every programmer who has compressed a file should read it.

Best forAnyone who wants to understand what a bit is.

No. 19 · Step IV

An Introduction to Mathematical Cryptography

Jeffrey Hoffstein, Jill Pipher and Joseph H. Silverman · Springer · 2008, second edition 2014

Every secure connection your programs make depends on a handful of mathematical problems that are easy to set and hard to solve. This book, by three mathematicians at Brown University who invented a well-known encryption system of their own, explains those problems and the systems built on them from the beginning.

It assumes very little. The number theory and algebra are developed as they are needed, alongside the cryptography that uses them: the exchange of keys between strangers, the RSA system and the difficulty of factoring, digital signatures, elliptic curves, and the lattice methods that are now important because they are expected to resist quantum computers. For each system the authors also show the best known attacks, which is the only way to understand why key sizes are what they are. If the number theory catches your interest, there is more of it in the best number theory books.

Best forProgrammers who want to know why encryption works, not only how to call it.

Step V · Books 20–23

Close to the machine

Last, the mathematics that lives inside the computer itself: the arithmetic of bits, the errors of rounding, the geometry of a rendered scene, and the algebra hidden in good code.

No. 20 · Step V

Hacker's Delight

Henry S. Warren, Jr. · Addison-Wesley · 2002, second edition 2012

Henry Warren spent a long career at IBM working on compilers and computer architecture, and over the years he collected the small, ingenious methods that programmers use to make machines do arithmetic quickly. This book is that collection, and the word hacker in its title has its old and honourable meaning.

It shows how to count the 1 bits in a word, reverse them, find the highest one, round up to a power of two, and divide by a constant without using division, each in a few instructions and each with a proof that it works. There are chapters on the arithmetic of integers of different sizes, on square roots, on unusual number bases, on error-correcting codes and on the curve that fills a square. The proofs are short pieces of real mathematics about whole numbers and logic. It is a book to dip into, and almost every page has something that will make you smile.

Best forSystems programmers, and anyone who enjoys a clever trick.

No. 21 · Step V

Numerical Methods for Scientists and Engineers

Richard W. Hamming · 1962, second edition 1973

Richard Hamming worked at Bell Laboratories in its great years, invented the first error-correcting codes and received the Turing Award. He put a motto at the front of this book that has been quoted ever since: the purpose of computing is insight, not numbers.

The book is about calculating with numbers that a machine can only store approximately. Hamming explains how rounding errors arise and grow, and then covers the standard tasks: solving equations, fitting curves through data, computing integrals, and following a system as it changes in time. A large part is given to his own approach through frequencies, which connects to the Fourier transform. What sets the book apart is its voice. Hamming keeps asking what the calculation is for and how you would know if the answer were wrong, and that is still the question most numerical code fails to ask.

Best forAnyone whose programs calculate with real numbers.

No. 22 · Step V · Free edition

3D Math Primer for Graphics and Game Development

Fletcher Dunn and Ian Parberry · CRC Press · 2002, second edition 2011

Fletcher Dunn is a game programmer and Ian Parberry a professor of computer science, and their book has taught three-dimensional mathematics to a generation of people who make games. It assumes only school mathematics and explains everything else with pictures, jokes and a great deal of care.

It begins with coordinate systems and vectors, moves on to matrices and what they do to shapes, and spends a long, valuable chapter on the several ways of describing a rotation, including quaternions, which every game engine uses and few programmers understand. Later chapters cover geometric tests such as whether a ray hits a triangle, the mathematics of lighting, the mechanics of moving bodies, and curves. The authors have put the whole second edition online to read without charge. For the linear algebra underneath it all, see the best linear algebra books.

Best forGame and graphics programmers who need the geometry, clearly explained.

No. 23 · Step V

From Mathematics to Generic Programming

Alexander A. Stepanov and Daniel E. Rose · Addison-Wesley · 2014

Alexander Stepanov designed the Standard Template Library of C++, the collection of containers and algorithms that changed how programmers think about reusable code. This book explains where his ideas came from, and the answer is the history of mathematics.

It opens with a method of multiplication used by Egyptian scribes almost four thousand years ago, which works by doubling and halving. Stepanov and Daniel Rose write it as a short program and then ask what the program really needs from its inputs. The answer is surprisingly little, and step by step the same few lines become a way of raising to powers, computing Fibonacci numbers and finding shortest paths. The second thread follows Euclid's algorithm for the greatest common divisor through two thousand years of generalisation. The book ends by assembling everything into the RSA encryption system. It is short, and it shows better than any other that good programming and good mathematics are the same activity.

Best forExperienced programmers who want to see how abstraction in mathematics becomes abstraction in code.

After the shelf

What to read next

Two subjects that programmers need are large enough to have pages of their own: the best linear algebra books, for graphics, data and machine learning, and the best probability and statistics books. The number theory behind cryptography continues in the best number theory books.

If the puzzles were what you enjoyed, there are many more in the best math puzzle books. More textbooks that cost nothing are in free math textbooks, and the whole road through mathematics is laid out in how to learn mathematics.

Beyond Euclid

Liked this? Get one like it every week.

One email a week, handpicked by Ali. 240 issues so far, free.