# What are good mathematical formulas to know for programming? [closed]

What are some somewhat common math formulas you learned that helped you write better algorithms and become a better programmer?

Example: I learned about the ecludian distance formula: `sqrt((x1-x2)^2+(y1-y2)^2)` which helped me understand how to find like objects by comparing 2 factors.

• I don't think that's the Euclidean distance formula. Sep 29 '10 at 4:06
• @Larry @Greg edited.
– GSto
Sep 29 '10 at 10:52
• haha how about the fibo sequence ... good for benchmarking Sep 29 '10 at 15:39
• Completion Date = (Estimated Date + Number of Days Left / 2) ^ (Time at Work / Time at Home) * Number of Free Pizzas Sep 30 '10 at 19:35
• you might find it interesting to know that if you just need to compare distances, you can skip the `sqrt` step. For a tight inner loop, that might matter.
– user1249
Nov 23 '10 at 20:37

Knowing the powers of 2 is handy, especially when dealing with low-level bitwise operations.

• +1 - Being able to convert to and from bases 2, 16, 10 and 8 is a must. Sep 29 '10 at 8:39
• I'm not sure about base-8, but I agree with 2,16, and 10 conversions. You should be capable of doing it in a reasonable timeframe, but not necessarily instantly. Sep 29 '10 at 15:29
• My one teacher is a hex-animal. Converts in his head ridiculous numbers and I was a TA for him for a year or so, the class was always impressed as was I. Sep 29 '10 at 16:20
• I hate base 8 :)
– user1827
Sep 29 '10 at 20:24
• With macros, enums and bitfields in modern languages, in what cases do people need to know powers of 2, which are fundamentally magic numbers.. `setsockopt(...SO_KEEPALIVE..)` is quite a lot easier to read or write than `setsockopt(...16...)` Jan 7 '11 at 16:01

Boolean algebra was already mentioned, but I wanted to provide some practical examples.

Boolean algebra comes in handy very often when you work with complex boolean expressions (in `if` statements for example).

Couple useful expressions and laws:

Distributivity

A & (B | C) = (A & B) | (A & C)

A | (B & C) = (A | B) & (A | C)

So next time you stumble upon such expression:

``````if((A || B) && (A || C) && (A || D) && (A || E)) { ... }
``````

You can easily shrink it to:

``````if(A || (B && C && D && E)) { ... }
``````

Negation and De Morgan's Law

!(!A) = A

!(A & B) = !A | !B

!(A | B) = !A & !B

Lets say you have such statement:

``````if(!A && !B && !C) {..}
``````

and you need to build the opposite of it. Writing:

``````if(!(!A && !B && !C)) {...}
``````

would work, but doesn't look as cool as this equivalent:

``````if(A | B | C) {...}
``````
• Problem with doing this, is if this is actual real-life business rules, as those have a tendency to CHANGE. If so, you need to reconstruct the original expression in order to change it, and then optimize it again. Maintainers tend to grumble while doing so.
– user1249
Nov 23 '10 at 20:40
• And that is where a Karnaugh map can help. It only works well on up to 4 boolean flags at once, but if you need more - good luck!
– Job
Dec 31 '10 at 4:28
• +1 for De Morgan's Law. I'm amazed at how few people seem to have been taught it and know how to apply it. Dec 31 '10 at 7:47
• You forgot this one: `(P -> Q) <=> (!P | Q)`. I use it all the time as very few environments offer a logical implication operator, this is a very handy equivalence for SQL CHECK constraints. Jan 1 '11 at 17:28
• Karnaugh and De Morgan's Law is something you're usually taught to use in electrical engineering classes but not in computer engineering classes. Which is odd, because applications can be found in the latter like the practical use of De Morgan's law mentioned above. Jan 25 '11 at 14:00

In my experience, Mathematical formulae are used for very specific calculations, which may or may not apply to your project.

If you need to calculate something, there is usually a function in a library or example source code around that can calculate it for you. For example, Excel's PMT() function, that calculates the payments required to repay a debt at X% over Y periods. Do you really want to have to know how it calculates it, or is it sufficient to just call the built-in one?

In the last 10 years, I don't think I've needed to use anything from the Math library other than Ceil(), Min() and Max(), which shows that even though computers were devised to solved math-based problems, the common use today is decision-making around the flow of data.

Take, for example, Facebook, which has a massive amount of code. There's probably some Math in there somewhere, but I suspect mainly in the Crypto API, which is probably a system library. But the database access, authorization decisions, page building and information routing probably don't use a whole lot of Math.

Yes, there are markets that need lots of Math - finance, physics, engineering - but in these industries, your primary discipline is more likely to be Math/Economics, Physics, Engineering, etc, so your questions would be 'how can I write formula f(x) in language Y?'

A better use of your time, IMO, would be to investigate Algorithms (including Big O notation) and Design Patterns.

• +1 because it seems like a reasonable statement -- there's no specific formula you should know, but the concept of algorithmic complexity (Big O notation) is very important. Sep 29 '10 at 16:38
• Plenty of math... Decides which ads to bug you with.
– user1249
Nov 23 '10 at 20:43
• I do agree the amount of math needed is generally quite low although my experience isn't quite as low as yours--I end up periodically using trig stuff in graphics. Dec 31 '10 at 4:45

There is no formula that can make you a better programmer.

Math related skills can make you a better programmer:

• Scientific method - math/science way of thinking and problem solving
• Abstraction - ability to recognize abstractions and patterns
• Inheritance - reuse of existing work/methods in solving new problems
• Experience - understanding a set of problems and solutions
• -1, the man asked about useful MATH FORMULAS. I can't believe this answer was upvoted at all.
– Jas
Jan 1 '11 at 0:15

Basic statistics formulas are good to know. I've used linear regression at least a few times.

I'd like to mention Taylor series which are quite useful to for getting quick approximations of "heavier" functions. For example `sin(x)` around 0 can be approximated with `x-(x*x*x/6)`.

In general, the idea that there are clever ways to approximate things quickly, instead of calculating them to the last significant digit (although for elementary functions, most modern processors contain fast hard-wired implementations so using Taylor to approximate sin may not be that significant speed gain).

De Morgan's laws, about transforming Boolean "and" and "or" relative to negations, and a few related more elementary tidbits about Boolean logic (such as double negation).

Rule of Three (type of Cross Multiplication)

+1 for Basic statistics formulas.

I saw many guys with difficulty to apply this simple rule on basics code.

• +1 for cross-multiplication. In some software that is prone to integer overflow problems, cross-multiplication is used to check that the results do not overflow. Oct 24 '10 at 16:18
• Not a big deal. This should be ingrained in the head of a high-school graduate who wishes to study CS.
– Job
Dec 31 '10 at 4:30
• @Job: In a theoretical world, this is true! Jan 8 '11 at 11:29

Sequence and series math.

I've seen too many schools teaching "write a loop to sum all the numbers between x and y" instead of "algorithms are AWESOME"

Law of Cosines, very important for a lot of geometrical problems, especially angle determination.

• what is gamma in that equation? Sep 29 '10 at 11:46
• @Matt Ellen: the angle of the side across side C (IOW, the angle between A and B) Sep 29 '10 at 11:49
• And of course, special case for right triangles: `a^2 + b^2 = c^2` Sep 29 '10 at 16:58

Programming is a very broad field. The math formula depends on which area of programming you are in. If you are into graphics, game programming you need to know more trigonometry, geometry. Game programming can be further categorized into areas like, physics, rendering, shader.. and the list goes on. So if you are a physics simulation expert, then you should know things related to Physics.
If you are into security, the you must be a Number Theory expert.
In general, you can go a combination of these, and which ever your interest is. Learning never hurts.

Methods of Proof

Most notably, the ones I've used with relative frequency:

There are more, and I have used many of them at one point or another, but these are the 3 that I can recall having used at a glance. They're also infinitely helpful if you can keep their intent in mind when writing unit or integration tests.

T(n) = aT(n/b) + f(n), a>=1, b>1

Master Theorem is good to know for programming. It lets you solve recurrence relations that can help you find the complexity of recursive algorithms. This is particularly important when writing a "divide-and-conquer" style algorithm. Roughly speaking, you can use the master theorem to get the complexity if you know the complexity of each "step" and the branching factor.

• why is it good to know for programming? Dec 31 '10 at 9:32
• @MattEllen: It lets you solve recurrence relations that can help you find the complexity of recursive algorithms. This is particularly important when writing a "divide-and-conquer" style algorithm. Roughly speaking, you can use the master theorem to get the complexity if you know the complexity of each "step" and the branching factor. Jul 30 '12 at 9:08
• algebra
• trigonometry
• vector (matrix operations)
• calculus
• [various interpolations and their derivatives]
• [surfaces, NURBS]

(the ones in brackers are more of an "applied" kind)

It is difficult to give general directions, since it strongly depends on the field you're in. But the above covers the basics of quite a lot of engineering degrees. Mind you, these categories often overlap (trigonometry+matrix ops., calculus+matrix ops., and so on.).

I always have a Mathematical handbook close by. One is often unsure of something, and it helps to have it presented in an organized manner.

Knowing boolean algebra helps a lot. It keeps you from writing code like

``````if (x < 10)
return true;
else
return false;
``````
• I am not quite sure I understand how boolean algebra helps prevent a user from writing that? Can you propose what the user should be writing there? (I would assume return x<10; but may be mistaken.) Sep 29 '10 at 16:19
• You are correct--it should be return x<10. Think of it this way. Evaluating (x < 10) will return a Boolean result. The if statement then breaks down to [if x is indeed less than 10] if(true) return true; or [if x is greater than or equal 10] if(false)...else return false; Sep 30 '10 at 21:25
• The (x < 10) may be separating two business cases. With the verbose form, you can do more than just returning values, which in maintenance mode is very nice, as you can keep changes to a minimum
– user1249
Nov 23 '10 at 20:45

For optimization problems, it's good to understand log-likelihood. For example, if you're trying to minimize a sum of squares, that's the same as maximizing the log of the likelihood, because (roughly speaking)

``````log( Product( exp( -(x[i]-mean)^2 )) )
=
- Sum( (x[i]-mean)^2 )
``````

Other favorites in the realm of performance tuning are Binomial and Beta distributions. They are very simple to calculate.

If you take take 10 random-time samples of the state of a program, and it is in a certain condition for F = 40% of the time, then it is just like a coin-toss experiment with an unfair coin. The number of times you will see it in that condition is a Binomial distribution with mean 10*0.4 = 4, and standard deviation of sqrt(10*0.4*0.6) = sqrt(2.4) = 1.55.

On the other hand if you take 10 samples and happen to see it in that condition on 4 samples, what does that tell you about how big F is? The possible outcomes are 0, 1, 2, 3, 4, ..., 9, 10. That's 11 possibilities, and the possibility you saw (4) is the 5th one. So, take 11 uniform(0,1) random numbers, and sort them. The distribution of the 5th one is the distribution of F, a Beta distribution. Its mode is 4/10. Its mean is 5/11. Its variance is 5*6/(11^2*12) = 0.021, and standard deviation = 0.144.

Many people think large numbers of samples are needed to locate software performance problems and avoid finding false ones. These distributions show that a small number of samples can reveal a lot about their cost.

This might be a bit simple, but `G=(V,E)` is a good one to keep in mind. In other words, a graph is a set of vertices and edges. Graphs are just so useful for representing lots of things.