How to debug math?

As a comp sci major who dabbles in math, let me throw my perspective into this.

Debugging math is a completely different experience from debugging programs. The difference is that while we have programs, they (tend to be) imperative - that is, they list out a series of steps that need to be followed.

(I know that this is a gross generalization, and that Haskell/LISP/Scheme/Functional Programming languages exist and they aren't that way, but most "general" programming languages are imperative.)

In math, you generally have a set of statements that you "bring together" to get a solution. It's more of an expression than a recipe if that makes sense to you.

However, the nice part is a lot of the intuition of debugging carries over.

Some principles that carry over very nicely:

  1. Isolate a problem, get the minimal failing example. This works in both math and compsci. If you have a general formula, substitute small values like $0$, $1$, etc. as a sanity check.

  2. Breaking things gives you a better understanding of how they work. When you read a theorem or code anything, always push the boundaries of what you're doing. Ask yourself - "what would happen if I do not have this statement in my proof"? "What if I eliminate this line"? "Is this assumption really necessary"? Playing with math and trying to break it often helps in learning it as well.

Also, some branches of math are just straight up weird, and require you to build intuition about them - You won't find the ideas intuitive at all when you begin (topology was like this for me when I began learning it), but you gradually pick up styles of thinking that complements the branch of math you're studying.

Another great "debugging tool" is to plug in good examples into theorems / problems whenever you face something too generic. Always arm yourself with at least 3 to 4 examples for any topic, so you can quickly fact-check about things (and refer back to those examples). It's also nice to have non-examples to things to know the difference.

If you've taken calculus, then knowing your examples would be

  1. a function that is discontinuous $f(x) = \text{floor}(x)$
  2. a continuous but not differentiable function - $f(x) = |x|$
  3. a function that is both continuous and differentiable - $f(x) = \sin x$
  4. a nowhere differentiable function - Weierstrass function
  5. a nowhere continuous function - Conway's base 13 function

There are books such as counterexamples in topology that will arm you with a whole bunch of examples to think about. Things like these are super useful the more abstract you get in math.

One last thing you can do as a programmer is to actually write code that checks a hunch you have - this is a huge advantage and don't be afraid to exploit this. Think something is true but feel too lazy to check it? Code it! It's not only practice, but it often teaches you some really cool math facts as well.

Hope this helps as a rough guide on how to debug math :)


A somewhat parallel to debugging would be to hand check the various intermediate steps in your reasoning for an instance of the problem that is small enough to work out everything on paper.

In your case "$4$ flips" is indeed such a small number. And it is easy enough to convince yourself that $4$ is the correct answer. So your second computation starts with only knowing things that are true under your assumptions and ends with a claim that you know is false. To find the fallacy you would look for the first step in the argument where you conclude something that is actually false.

The first step in your reasoning is that there are $4\times 3\times 2=24$ ways to pick the three tails positions, as long as you remember the order you choose them in. We can verify that by writing down those 24 ways and counting.

123       124       132       134       142       143
213       214       231       234       241       243
312       314       321       323       341       342
412       413       421       423       431       432

Count - there are 24 of them, and if you've been a bit systematic about it you're fairly sure that none have been forgotten.

Then next step is that each TTHT-like outcome appears for 3 of these situations. Let's check that that is true by noting down what the outcome is in each of our list of 24:

123 TTTH  124 TTHT  132 TTTH  134 THTT  142 TTHT  143 THTT
213 TTTH  214 TTHT  231 TTTH  234 ...   241       243
312       314       321       323       341       342
412       413       421       423       431       432

Whoops -- now we have found four combinations that lead to TTTH, but according to our claim there should only be three. So there must have been an error in the reasoning that concluded that each outcome appears only three times!


I'm a student currently learning math at the undergraduate level. I mostly learn by self-study, so "debugging" my thinking process during the learning process is crucial. (I will interpret "debug" a little less literally, so more as checking your own work using alternate solutions / shortcuts.) This list is not exhaustive, but off of the top of my head here are a few ways I "debug" my work in math problems:

  • Is the solution of the right magnitude? (There are $2^4 = 16$ possibilities after flipping a coin $4$ times, so a solution of $40$ to your problem would not make sense.)
  • Look at the divisors of the solution. Are they "reasonable"? (Here it makes sense that your solution is divisible by $4$, because here are $4$ "spots" for the H and it intuitively shouldn't matter which spot it occupies.)
  • If computing a part of the whole (combinatorics), can you compute the other parts relatively quickly? If so, does the sum of the parts equal the whole?
  • When checking a proof: did you use all of the assumptions or was the proof "too simple"? If not, you've probably oversimplified something. (For instance here, why it's important that $a$ is squarefree in order to prove that $\sqrt{a}$ is irrational. )
  • For combinatorics: double counting.
  • Whenever possible, try to gain understand a concept / problem in multiple ways. Your solution is more likely to be correct if it passes both checks. For instance in calculus, most ideas can be understood geometrically and algebraically.
  • Since you're using the term "debug", you're probably a programmer. Write programs to check your work when applicable. (Combinatorics, number theory, even more specialized areas if you check out software like sage. )
  • When working with functions, make sure the simplest cases make sense. (E.g.: does it make sense that $P(x) = -32 + 4x$ models profits selling $x$ units at a price of $4$ dollars apiece with initial cost $32$ dollars? Yes, since $P(0) = -32$ is negative (before selling you lose money on fixed costs), and $P(8) = 0$ is where you break even, just as expected.)
  • For anything with a geometric interpretation: try to make an accurate visualization (by hand, or using something like GeoGebra or WolframAlpha) and see how your solution holds up. (E.g. did you have a triangle with sides $1$, $1$ and $3$ that is not possible to draw?)

The best way to learn to debug math though is doing practice problems / proofs. The experience of bashing your head against a problem until you get it helps build intuition in that small area. And part of intuition is knowing which random tricks are best to "debug" that particular problem.