# How important is math in computer programming?

### Douglas Makey Mendez Molero Jan 08, 2018

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 {
fmt.Println(“YES”)
break;
}
}
```

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 {
fmt.Println("YES")
} else if x1 == x2 && v1 != v2{
fmt.Println("NO")
} else {
if v1 > v2 && ((x2-x1)%(v1-v2)) == 0 {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
```

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.

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 code without making a spaghetti mess, is a really important skill to have, and it does not require any knowledge of mathematics.

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 (an 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.

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.

Thatis far more important than who's right or wrong in this case. :DCheers!

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).

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.

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?

I don't really think CS = Web development.

CS is the science of computation, intersection between Maths and Formal Logic.

You really don't need a CS degree to do web stuffs.

But you probably need maths everything CS related.

Best answer here.

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.

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.

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 ...

source

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.

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.

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.

Please correct me if I am wrong but won't your answer print "No" for the following input (0,12,4,2).

Here x1 = 0, v1 = 12 and x2=4 and v2=2.

First Kangaroo x1 reaches 12 in 1 step & second Kangaroo reaches 12 in 4 steps.

But your solution will print "No".

I guess the question is should the both take the same number of steps to reach the common point.

Good Maths base is a must, but you don't have to be THAT good. Just some basic knowledge usually is enough. What you have to be good at is solving math problems 😀

Math is very important. It help us to make solution looks cleaner. I don’t agree with “good developer is not good at math”

The best programmers I know are very good at math!