Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share

locked by maple_shaft Aug 4 '13 at 22:53

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as not constructive by ChrisF Feb 9 '12 at 22:53

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
I don't think that's the Euclidean distance formula. –  Greg Hewgill Sep 29 '10 at 4:06
1  
Completion Date = (Estimated Date + Number of Days Left / 2) ^ (Time at Work / Time at Home) * Number of Free Pizzas –  Skizz Sep 30 '10 at 19:35
7  
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
show 2 more comments

17 Answers 17

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.

share
1  
why is it good to know for programming? –  Matt Ellen Dec 31 '10 at 9:32
show 1 more comment

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) {...}
share
2  
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
3  
+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. –  Fortyrunner Dec 31 '10 at 7:47
show 5 more comments

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.

share
add comment

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
share
show 1 more comment

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.

share
add comment

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.

share
add comment

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.

share
add comment

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

share
add comment

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

share
add comment

Law of Cosines, very important for a lot of geometrical problems,

alt text

especially angle determination.

share
1  
@Matt Ellen: the angle of the side across side C (IOW, the angle between A and B) –  Lie Ryan Sep 29 '10 at 11:49
2  
And of course, special case for right triangles: a^2 + b^2 = c^2 –  Note to self - think of a name Sep 29 '10 at 16:58
show 2 more comments

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"

Also... https://docs.google.com/viewer?url=http://courses.csail.mit.edu/6.042/fall10/mcs-ftl.pdf

share
add comment

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

if (x < 10)
    return true;
else
    return false;
share
1  
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; –  Eric Sep 30 '10 at 21:25
2  
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
show 1 more comment

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

share
2  
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. –  Incognito Sep 29 '10 at 15:29
1  
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...) –  JBRWilkinson Jan 7 '11 at 16:01
show 6 more comments

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.

share
2  
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
show 2 more comments

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.

share
1  
+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. –  khedron Sep 29 '10 at 16:38
show 2 more comments
  • 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.

share
add comment

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

share
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.