DEV Community

Douglas Makey Mendez Molero
Douglas Makey Mendez Molero

Posted on • Updated on


How important is math in computer programming?

I’m writing this article because I recently did this exercise in HackerRank:


I don’t have a degree in computer science or similar (but i’m working as a software engineer the last 3 years) so I really don’t have a solid math knowledge and at first sight this exercise seems easy, right?

Most of you will think that it is, but to me it wasn’t. My first approach was to think in a logical solution (at least I’d like to think that it was), then I thinked that if i wanted to know if the kangaroos will ever land on the same location at the same time I would have to move the kangaroos until they were both on the same location

I designed a loop that would move the kangaroos and check if they are on the same position.

Forgetting other validations, I only wrote the necessary code for the case:

for i:=0 ; i<10000; i++ {
    kOnePosition = moveKangoro(x1, v1)
    kTwoPosition = moveKangoro(x2, v2)
    if kOnePosition == kTwoPosition {
Enter fullscreen mode Exit fullscreen mode

I wrote variants of this code and other ones, but neither of them passed the tests. After 10 or 20 minutes I thought in a math solution.

The equation to verify if in n jumps the kangaroos are going to be on the same location is this:

x1 + (v1 * n) = x2 + (v2 * n) -> where n is of num of jumps.

So, we are going to resolve this equation:

(v1 * n) — (v2 * n) = x2 — x1

n(v1 — v2) = x2 — x1

n = (x2 — x1) / (v1 — v2)

We know that the value of n needs to be an integer, so we’re going to replace the division by the modular division and check if the operation leaves a remainder of 0

(x2 — x1) % (v1 — v2) == 0

This means that for a set of initial positions and meters per jump, we can know that the kangaroos are going to be on the same position at the same time only if the remainder value is 0.

Again, I’m forgetting other validations, I only wrote the necessary code for the case:

if x1 == x2 && v1 == v2 {
   } else if x1 == x2 && v1 != v2{
   } else {
       if v1 > v2 && ((x2-x1)%(v1-v2)) == 0 {
       } else {
Enter fullscreen mode Exit fullscreen mode

This algorithm passed all the tests and we could only do this efficiently with maths.

Sometimes I hear developers say “Math is not very important” or phrases like “A good coder is bad at math” and I think that they are wrong because as we can saw in this case, maths helps us to solve a problem in an efficient way.

I’ve a lot to learn and It’s exciting to find these problems to improve every day.

Code in Github

Top comments (37)

leob profile image
leob • Edited

I'd say a formal math education is totally unnecessary for 99% of the programming tasks, however people who are good at programming will in general also be good (or at least okay) in math. To be good at programming requires the ability to think in an abstract, analytical and systematic way, and that goes for math too.

On the other hand, I've read (and believe) that performing programming tasks activates the "language" part of the brain rather than the "math" part of the brain.

So I'd say there are ties or commonalities between the two but I wouldn't overstate the importance of math for being a good developer in general.

The story changes of course once you venture into numerical/scientific programming or data science.

ionutarhire profile image
Arhire Ionut

Hi! Could you tell me where you read the thing about programming activating the "language" part of the brain please? I'm interested about it.

leob profile image

Yes absolutely, I don't know where I read it originally but it was easy to google it. Scientists put programmers in an MRI scanner and then analyzed which brain centers were activated when they started to study/read source code. Their conclusion was that primarily the "language" center of the brain was activated.

In case you'd want to read the original research paper: however here is a good layman's summary:

and another one:

and some discussions:

Now, my take on it is that I'm not surprised by this outcome, not just because "programming language" has the word 'language' in it, but also remember the phrase "well written software reads like prose" (or like a novel).

Also remember the importance of stuff like choosing good (meaningful) variable names, and so on. Having an excellent command of native or natural language has got to be a foundational pillar for at least a large part of programming.

On the other hand, I noticed that there are many articles on this topic, but all of them seem to refer to the same scientific paper with the MRI scanner, so the scientific basis seems to be a bit narrow (only one group who researched it), but it seems reliable enough.

However, the scope of the study is fairly narrow - only reading source code. What about writing, what about debugging, testing, what about other activities associated with 'programming'? I dare to make a prediction that additional "centers" will be activated, but these kind of scenarios weren't scientifically studied (with MRI and whatnot).

Also, reading source code may be a little like reading a book (detective novel?) but grokking a complex system also seem akin to taking apart a complex piece of machinery.

So all in all I believe the basic premise of the theory but I'm convinced that it's only part of the story and the full picture is a lot more complex and nuanced.

Thread Thread
ionutarhire profile image
Arhire Ionut

Thanks a bunch! I agree with you entirely. I read the articles and the thing is, we're just going to have to wait for more in-depth studies to come out to form meaningful opinions on the subject. Still a very interesting read and I recommend it to any developer on here.

craser profile image
Chris Raser

I think this is a case of everyone being generally right, but saying the same thing in different ways.

Having been a dev for ~17 years, I agree that most tasks I perform each day can't be expressed in pure mathematics as easily as the kangaroo problem above.

Also having been a dev for ~17 years, I agree with you that for some problems, a mathematical solution is superior to an algorithmic one.

You seem to see clearly the relative importance of mathematics in software development. That is far more important than who's right or wrong in this case. :D


tanjent profile image

I would say that a strong understanding of algebra, functions, and boolean logic/truth table work is pretty much required for any developer.

Beyond that, it really depends on the type of work and the problem domain you work in. Certain technologies, and certain problem domains can require additional mathematical skills for you to understand the problem you're solving, and be able to relate to it in your code.

For instance, someone heavily involved in relational databases needs a strong understanding of set theory (the basis of SQL). If you're working in a bioinformatics realm, you need a strong linear algebra and statistics background. If you're doing real-world modeling (eg, simulating for airflow over a wing), you'll need to understand the physics involved (and hence the math required by that physics).

thetuftii profile image
Claire Pollard • Edited

It depends on the type of programming job you have I'd say. For mine working on CAD/CAE software it's critical as I have to read/write academic research at times to implement complex/unique algorithms to do a particular job. It's fairly maths intensive at times, with lots of geometry and topology.

Also, Mathematics is more than just numbers and equations, it provides you with a logical way of breaking down a problem and gives you a toolbox of thought processes to use when trying to solve a problem.

So I'd have to sit on the fence for this one. I'd say it's not essential for all dev jobs, but can help for some software development roles.

6temes profile image
Daniel • Edited

It really depends on what you are programming. If you want to become a video-games developer, you absolutely need to know maths. Specially geometry and vectors.

For Neural Networks, you will need to know calculus in order to understand the papers.

If you are a web developer and work with a high level programming language, literacy skills are the most important because your main job will be, not making the computer do what you want, but understanding other developers' code and write code that other developers can understand.

Being able to code a big amount of business logic without making a spaghetti mess, is a really important skill to have, and it does not require any knowledge of mathematics.

mortoray profile image
edA‑qa mort‑ora‑y

Algorithms are math. Logic is math (and philosophy). Finance is math. Statistics is math. Metrics (QA, Marketing, Business) are math.

Math is a large field of study. There are obviously portions of coding that have nothing to do with math, but the vast majority of it involves math in some way.

I find it hard to believe that somebody could be a good coder without being good at least some aspects of math.

Be leary of coders who say they don't know, or don't like math.

xicarus profile image
Sergiu-Lucian Petrica • Edited

"Be leary of coders who say they don't know, or don't like math."

This is a silly statement, how does a preference to a potentially unrelated field of study relate to the skill of the developer?
Web developers very rarely need any advanced math concepts for example. If you're not gonna hire a web developer because he doesn't like maths you're gonna have a hard time hiring. In fact from my personal experience most developers don't like maths. In Romania they go to Informatics universities as opposed to Mathematics-Informatics ones in order to evade a large part of maths.

mortoray profile image
edA‑qa mort‑ora‑y • Edited

Do you disagree with my assessment of the breadth of the math discipline? Or are you really saying that a coder could get away without know any math what-so-ever?

Thread Thread
xicarus profile image
Sergiu-Lucian Petrica

I said advanced mathematics, you obviously can't get away as a programmer without knowing basic maths.
However generalizing that a programmer who hates maths is a programmer you should avoid is beyond silly. It depends on the subfield. If you're gonna hire a game engine developer who doesn't know trigonometry or linear algebra of course you're gonna have a bad time, but if you're hiring a mobile developer you don't care how good he is at calculus.

patricktingen profile image
Patrick Tingen • Edited

Math is important, but not because of the mathematics.

Instead, it is important because it teaches you to think structured, so math is not the aim, it's the means. It teaches you analytical skills that you need to solve programming problems.

Because jumping kangaroos are rarely interesting ...

Wumo of today

cyberspots56 profile image
John Rainey

First, I would say that Discrete Mathematics is very important. You learn Boolean logic and set theory. Nice things to know when writing software. From my own personal experience mathematics has been quite valuable. For example, you are working on an application that requires the best possible performance. You have two algorithms that you are considering, but which one will be faster? I have used mathematics extensively to make this decision. It's so much easier to do a little calculus than a series of tests with actual data. I spent my career working on apps where execution time was vital. Using a little math always seemed easier to me.

dougmckechie profile image
Douglas McKechie

How important is math in computer programming? My first reaction would be "not much" as I've never needed to do much math as a web developer. But I suppose the importance of math depends on what sort of development is being done.

Interestingly I just mentioned to my sister the other day how often people used to assume I was good at maths because I worked in IT (which is not the case). Does anyone else have that happen to them?

Sloan, the sloth mascot
Comment deleted
ivxxcatto profile image

I'd like to add that most people think that math is all about equations and formulas, but it really is much deeper and beautiful. Math is the study of how things work and ultimately how the human mind things and deals with complexity.

You are right that math is very useful (it also depends on what kind of coding you do) but it is also very interesting and beautiful by itself when you learn it the right way. I think many people dislike, fear or misunderstand math because it is not taught correctly in schools.

Congrats for being eager to learn and improve your skills, you are on the right track here.

Finally, I would like to recommend you a coupke of things:

The paper "A mathematician's lament" by Paul Lockhart talks about what's wrong with how math is usually taught. A very nice and entertaining read.

The books "The art of computer programming" and "Concrete Mathematics" by Donald Knuth and others ate simply awesome. Try Concrete Math first as it has the math needed to read the art of computer programming and it really makes you think a lot, it is hard but rewarding and beautifully written.

databasesponge profile image
MetaDave 🇪🇺

In the last 25 years, the only serious mathematics I've used was part of a forecasting system, in which I had to take forecasts of A, B, and (A-B), where A & B were a couple of orders of magnitude greater than (A-B), and adjust the forecast values of A and B (lets say Af and Bf) in order that the (Af - Bf) was equal to the the forecast of (A-B).

Now I didn't do the forecasting, I just had to do the adjustment, and it turned out that to so I had to solve a quadratic equation using the classic method.

In Informatica Powercenter.

vithabada profile image
Vít Habada • Edited

Law of diminishing returns.

You can be a great programmer without the formal knowledge, sure.

Learning all the math and computer science stuff is hard. And most of the time you don't need it (compared to other coding-related skills others mention), but when you do - boy oh boy it matters alot. Your simple excercise is a great example.

So on the importance - the answer is as usual 'it depends'.

I think most important, generally, is to understand how things that you use work. That includes some math, but more importantly it includes knowing how memory is managed, how the VM that's running your javascript code works and what sort of optimizations it does and so on.. Once all that knowledge fits together it feels really good to know exactly what you are doing.

maxvonhippel profile image
Max von Hippel

It completely depends what you are doing. I used Quaternion.slerp in Unity3D on an almost weekly basis in my part-time job for 2 years during high school, and only just learned what Quaternions were and how to use them mathematically this year (and I'm a junior pure mathematics major in university now).

Likewise, you could make it pretty far into using something like Torch for AI/ML without learning abstract or linear algebra, I think.

Understanding boolean algebra - which is a lot simpler than it sounds - is very useful. It lets you write shorter and more concise logical statements and better understand overall how computers work. It is absolutely essential to even starting to understand computer hardware at the chip level - for example, CPU design. But a lot of people work for years and years as software developers/engineers and never encounter any of that stuff.

In university, I often learn more in my free time on Stack Exchange and from extra textbooks than in class. Classes provide tests, structured learning, and homework sets, but at the end of the day we have to learn a lot of the material on your own. So not going to college for math or CS isn't necessarily a big deal - it just means you have to learn a bit more on your own than most people.

One area where I feel math really helps is any sort of scientific modeling. I can't imagine doing modeling without having 2 semesters of fairly advanced linear algebra under my belt. That said, a lot of statisticians do similar work from a statistical perspective, and I'm not sure that they all know about metric spaces, vector spaces, fields, groups, etc. etc.

yaser profile image
Yaser Al-Najjar • Edited

Let's just cut the crab...

I have taken lots of practical math cuz I enrolled in computer engineering (Calculus 1 & 2, Linear Algebra, Statistics & Probability, Differentail equations, physics 1 & 2)

And hell no, I have never used those for the 7 years passed in building enterprise software.

quii profile image
Chris James

I'm saddened by people being dismissive of mathematics here but I think it's because a lot of people dont see maths as being much more than arithmetic and statistics.

I strongly urge you to look at Category Theory, which is a branch of maths.

It is a broad area which is concerned about formalising abstractions, such as sets.

Formalising abstractions Does that sound familiar? If you have any interest in functional programming you owe it to yourself to investigate it.

Category theory has practical applications in programming language theory, for example the usage of monads in functional programming.

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<