“What does mathematics have to do with programming?”

A post I have wanted to write for a long time, even as a not-great-at-math person

Curt Corginia
CodeX

--

I have been delaying this post for weeks and weeks, even though I have wanted to write it since finding this gem on StackExchange. The question of why we need math to become programmers is a contrived one, but someone answered how they relate to each other in an interesting way:

First off: I am a mathematician — a professional one (in that I get paid for doing maths). I am not a programmer. I do do some programming, but very definitely of the Cargo Cult variety (see first comment to https://tex.stackexchange.com/q/451/86 and my response) and nothing of the sort that would normally bring me to this site (indeed, I registered here to post this answer after seeing a link to it in the TeX chat room).

The summary of my answer is: Mathematics is Programming.

You can learn all of these things [abstraction, perspective, form vs. function] from being a good programmer. And if you’ve learnt these things, you will find mathematics much easier because you will understand that when we talk about a vector in a vector space then it’s just an instance of the class Vector which means that we can do all the things that Vector does to that instance: add, subtract, scale, and so forth. That's why I would love to teach mathematics to programmers. But, speaking as a mathematician, I would say that the first of these, "Abstraction", is easier to learn in mathematics than in programming because mathematics is the pursuit of abstraction

I do not want to say whether I agree that mathematics literally is programming — I think this would mostly be a terminology debate. I also do not want to talk about math and programming in terms of the brain, which they already did in this SciShow episode to ask if coding was more related to math, or linguistics. What I appreciated about this post was its positive attitude, and the non-condescending, enthusiastic way it encouraged programmers to pursue math. This kind of energy, I feel, is missing a lot in the industry. It seems especially relevant now that I have this blog, which focuses on coding interview questions. Coding interview questions feel a little more “math-y” than the more practical day-to-day demands of professional programming.

Like centering a <div>.

He also ties in Lockhart’s Lament, an essay linked in full here, which argues that the way we teach math in school is wrong because it discourages exploration, disincentives creativity, and makes the whole subject boring when it should be as inspiring as an art form like painting and music.

The Interview Question

You have a drone located somewhere on an XY coordinate plane, and you receive a series of XY coordinates for ships you would like to take pictures of. You need to take pictures of all ships. You receive a field of vision for your camera, and you need to return a list of camera yaw angles for the photos you will take.

This was a one-day coding challenge, and it had to be C++. I will not provide any code, or my answer, because I strongly suspect it was the wrong answer. I was thinking greedy algorithm. Point your camera exactly at one of the boats. Take a picture. Now move again until you reach the next ship that was outside of your field of vision.

This is not optimal in every case.

I found some interesting things in the process of doing this, though. I think the trigonometry is high school level, if not middle school level, but you use arctan to turn XY coordinates into angles. I realized the instructions were treating 0 degrees as north, and going clockwise, which definitely was a frustrating setback. The correct formula is actually the one in the picture at the top of this post, drawn in crayon.

Asking Around

Treating programming as a form of math legitimizes the effort. Math is difficult — it requires meticulousness, precision, and the ability to concentrate on problems that seem to stray from the outside world into their own internal logic. Math is so difficult and confusing, in fact, that one of the only people I met who enjoyed it was the woman who lived across from my dorm in college. She was a math major, for some reason.

I would say [that they are similar]. I like puzzles and solving problems, which are certainly in the center of the Venn diagram. Sorry I can’t give you anything more quotable. [Please make my alias] Rocky.
— Rocky

So that was her response. Most of the other people I asked were a little more lukewarm about it. Incidentally, one of them lived next to my dorm in college. This was quite a floor.

The fourth floor was the best floor. Good times. Not only were those regular brownies, they were regular sugar-free brownies. This April 1 joke was the 7th most unethical thing I have ever done.

Me: Is coding actually just math? I mean, you’re declaring variables. You’re mutating state. You’re defining inputs and outputs
GSkittles: Huh, I’d say it’s more formal logic. But then again, isn’t math as well? Maybe coding and math are like cousins, rather than direct ancestor/descendant. I dunno, I press buttons on the computer until the app works and they give me money

This is too many aliases, even for my taste. Tony simply asked if we mutate state in math. Chris, who asked to have the alias “Crispy,” said…

…no, not really.

Say you were tasked with finding the minimum contiguous subarray that sums to a target. You determine a solution, but you want to achieve an asymptotic complexity of O(n). You recognize that this is a Sliding Window Technique problem. You follow the technique of expanding right, then expanding left until the condition is no longer met. You get hired at Meta, you brag about your salary on Reddit, LinkedIn, and OKCupid, and you are happy for a while until they realize you do not know how to center a <div>.

The technique consists of some math. The precision required seems to match the spirit of math. But this is still not quite the same as solving a math problem, and a big debate in the tech community is whether computer science students should really be required to take three semesters of Calculus, one semester of linear algebra, and upper-division classes like combinatorics.

An Intellectual Pursuit

I remember when I was reading My Life as a Quant, and I remember being impressed by how much enthusiasm it had in it. He talks about his fascination with physics, and math, and finally the stock market. I told Smack, someone who is probably a better programmer than I will ever be, about it. Our conversation went something like this:

Me: Let’s have intellectual conversations from now on
Smack: Ah, yes. Will we also enjoy the smell of each other’s farts?
Me: …You think this is pretentious?
Smack: Indubitably

Well, hear me out. What do we think of, when we think of coding interviews? Mostly arbitrary bull***, a term I admittedly used myself about algorithm problems. I wrote that we can call AR a metric of how much ARbitrary bull**** an interview consists of, and US as the percentage of how much Useful S**** it consists of. System design interviews and interviews where they actually have you solve relevant industry problems, like how to center a <div>, rate very high in this.

Maybe this is a good attitude adjustment. Let’s say you’re writing simple JavaScript, in a frontend, to render navigational data sent via server-sent events. Nothing is happening. Well, at this point you can think of it as a very logical problem. You have your smaller program, and it is taking variables, modifying them using functions, and then carrying out an action. Because it is an undesired action, you fan out. You check other things that may be a factor, you problem-solve, you learn relevant facts about how these variables transition, how they are used in typical cases, what they are doing here. Finally, you realize you were applying the wrong function, verified when you actually see the points drawn on the map.

This problem may seem easy, especially to the rest of your team, but it is actually reasonably complex. You were using a library you did not invent to apply an operation, mapping, which is something you have not done yourself (at least in this hypothetical example). There are moving parts. It was a reasonable way to spend your time, and you are not an idiot for having this bug.

A lot of programming consists of a constant voice that says, “What are you doing. This is stupid. You are an idiot.” But this kind of work, particularly in the industry, consists of many variables (both figuratively and literally). Comparing it to math is just another lens, and it’s motivating because it reminds us that we occupy a domain that is dictated by logic. If a single thing goes wrong, the entire thing can collapse. This is not always easy, in fact it is often very difficult, and that makes it a worthy challenge.

Closing Thoughts

Some people, like Rocky, had a pretty easy time getting into coding. Yes, sometimes it was challenging. Yes, it definitely was work. But she was better because of it. The challenge was meaningful.

At this point, I have about nine interviews I can write to you about. Sample code saved on repl.it for a JavaScript challenge. Sample code saved on repl.it for a Java challenge. There’s that O(1) insert/delete/getRandom challenge I keep talking about, which is a LeetCode medium, as well as a FAANG interview I will share after I read the NDA (or not share, if that is forbidden by the NDA). There was also another 2Sum variation, which I will have to do by hand because I am unwilling to pay for LeetCode premium.

It’s a long path. Not everyone wants to walk it.

I can only dream of having the maturity of whoever answered the prompt on StackExchange.

Good luck, everyone!

--

--

Curt Corginia
CodeX
Writer for

Founder, CEO, CTO, COO, and janitor at a company I made up called CORGICorporation