DEV Community

Cover image for Why Math Helps Me Every Day as a Programmer
Christian
Christian

Posted on

Why Math Helps Me Every Day as a Programmer

This is gonna be less technical than some of my other posts on this blog. But it occurred to me when I was trying to learn some more things about creative coding how much a degree in Math helps me as a programmer.

Now, obviously there are more apt majors for a programmer to have (like C.S. or electrical engineering), but I find that my background in Math really helps me three core ways.

1. Patience

There was a circulating trait among math majors when I was in college. When studying for midterms or finals, it was routine for us to hammer the core math proofs into our noggins to be demonstrated on a test. My junior year class in real analysis had us students go over the fundamental theorem of calculus and sure enough, that we had to rewrite it step by step on the final.

At first the steps seemed complicated and almost fragmented in the way that the logic was distilled into core components. This is because years and years of mathematicians edited down Newton's original proof until you could pretty much kick it any way you could and the thing wouldn't budge. This meant that its details are almost inscrutable to the single take. Math students had to pore over theorems like this, in pretty much all abstract math classes, for serious amounts of time to really understand it.

And I really think this kind of study, where your eyes glaze over from looking at the difference between two lines of a proof, lends itself super well to programming as a whole. Programming when you encounter a new challenge or learning a new framework will punish someone who refuses to pay attention to the small details. When a bug is lurking in your code, a programmer requires patience to look line-by-line where things went wrong, the very same way you glean the workings of a math proof.

2. The Flow

In addition to regurgitating the famous math proofs, assignments and exams have students use the topics of the course to prove, unequivocally, the truth of different mathematical statements. These were way less important than the core theorems, but required students to use what was already known and extend it that much farther. When push came to shove on a test where you were required to prove something you had never glanced at before, there was an interesting feeling that occurred.

In some ways, I personally would know what the blueprints of the proof would be. In some sense my intuition would guide the steps of the solution through until the contours of the proof were filled in. And now, four years out from graduating, I feel this same feeling when practicing or completing the dreaded coding interview questions. There is a certain amount of intuition or forward thinking that math cultivated in me to really help with coding interview questions. It developed in me a comfort in flowing towards a solution and letting intuition bubble up to lead you forward.

Also, math proofs taught me to focus on the pattern of the solution rather than the actual steps. In a lot of ways the coding interview questions have recurring patterns in their solutions. Whether it be resorting to a hash for fast lookup, using divide and conquer or greedy approaches, or binary search, these are all tools and patterns of approach for problem solving. To understand when to use them won't come from memorizing 100 algorithms but feeling them intuitively while working towards a solution.

3. A Love of Elegance

Finally, the last thing I think transfers so well from a study of math into programming is an appreciation for simple or elegant solutions. When you spend 2 or so years looking all sorts of proofs, you gather an appreciation for "beautiful" proofs as it were. A beautiful proof for me is one where you get the same kind of "I could have thought of this!" But the truth is, you couldn't have.

A beautiful math proof flows out from the problem and almost solves itself in how simple the steps are. There is no fat or fluff whatsoever. You can sense the brilliance of the mathematician who solved it. Its logic is also not hard to penetrate either. Likewise, beautiful code follows much the same sort of precepts. Simple and readable code is enjoyable for others. Elegant solutions to coding problems don't wind all around but are economical.

Runner-Up: Equations and Stuff

I wanted to make this post more about the implicit things that help me as a programmer and not the actual math-y stuff. But, that of course does come in handy sometimes. Especially when you're actually using sin, cos, spheres, shapes, rotations, matrices, or you name it for generative art. Processing and P5 have really well developed math stuff built into them. And what's nice about having a background in math is that this sorta stuff is really comfortable for me to work in given the ungodly amount of hours I worked with them in college. Like check out this moving sphere I built from the coding train.

Sphere

import peasy.*;

PeasyCam cam;
float zoff = 1;
color[] pallette = new color[4];

void setup() {
  size(800,800, P3D);
  cam = new PeasyCam(this,800);
}

void draw() {
  background(65,67,105);
  lights();
  float r = 200;
  int total = 50;
  for (int i = 0; i < total; i++) {
   float lon = map(noise(i,zoff), 0, 1, -TWO_PI, TWO_PI);
   beginShape();
   noFill();
   for (int j = 0; j < total; j++) {
     float lat = map(j, 0, total, -HALF_PI, HALF_PI);
     float x = r * sin(lon) * cos(lat);
     float y = r * sin(lon) * sin(lat);
     float z = r * cos(lon);
     stroke(255);
     vertex(x,y,z);
   }
   endShape(CLOSE);
  }
  zoff += .001;
}

Lots of math-y goodness all around.

I hope this was enjoyable to read and possibly encouraging for a math major wondering how their skills could transfer over. I remember being a little discouraged coming out of college wondering if software was for me. I wish I'd gotten into it more back then but it's never too late.

Top comments (2)

Collapse
 
x777 profile image
YD

Yes, I regret that was not good in school on Math lessons

Collapse
 
moustaphacheikh profile image
Moustapha Cheikh

Well written article. Having patience while problem solving and learning is my favorite thing. It is actually an enjoyable part of programming when i stuck on a problem.