I was talking to someone the other day on IRC, and they were talking about how he saw his math class as being a waste of time. Too theoretical, not practical. It’s usually hard to see the benefits, until you realize that it opens up opportunities on how to solve problems.
Take today: I wanted to calculate Pi to a certain number of digits. However, I didn’t know how much computer storage, say, a trillion-digit long number would require. Math to the rescue!
We know that with one bit of computer storage, we can store a one or a zero. With each additional bit of computer space, the biggest size that that stored number can be doubles. For instance, with three bits you can represent all the numbers from zero up to (but not including) eight, but with four, you can represent all the numbers from zero up to (but not including) sixteen. Thus you can use this formula:
Where maxval is the biggest number you can store given a number of bits. That maximum value is a decimal number. Keep in mind that all decimal numbers can be represented in the scientific notation:
Where b corresponds to the length in digits of that number. So maxval, in fact, also equates to the following:
We’re looking for the number of digits in multiple of tens – and therefore a will always equate to being 1. As such, we can simplify the above formula to:
And thus, since both the first formula and this last one equal to the same thing, we can stipulate the following:
Since we want to find out the amount of bits given a known number of digits, we need to rewrite the formula so that we end up with the amount of bits. This is where this rule from high school math comes in handy:
And using that rule, we can rewrite the formula to figure out the amount of bits as such:
We could stop there. Given the length of a number in digits, we could figure out how many bits would be required to store it. That said, many calculators don’t have the ability to make calculations with logs at base 2. Most calculators are able to however calculate logs at base 10. This is where another high school trick to change bases of logarithms comes in handy:
Which is applied to our formula to convert it to the following:
Finally, the above can be simplified. Keep in mind that:
So the logarithm in the top portion of the equation actually equals digits in itself:
Keeping this in mind, the whole formula can be rewritten as such:
And since a logarithm without an indicated base is assumed to have base 10, the above formula can be further cut down:
And that’s it! Thanks to a little bit of math, we can figure out how many bits of space would be required to store a certain amount of digits. So, going back to the pi example – how much storage space would be required to store pi to a trillion digits?
Storing pi to one trillion digits would require approximately 3.3 trillion bits of computer storage, or around 387 gigabytes! Problems like the one exemplified here aren’t uncommon, and a solid foundation in mathematics can go a long ways in opening new doors as to how to solve them.
So whether you’re taking calculus, linear algebra, numerical analysis and/or statistics – remember: that theory you’re learning will come in handy one day.
Comments
4 responses to “Why Math is Important for Programmers”
Jesus H….you lost me at maxval.
Haha… Yeah, I completely geeked out for this one >.<
Might I suggest a book called Hacker’s Delight by Henry S Warren. The book is all about using math to better yourself at programming. Very fascinating book.
You LOVE Math It helps you solve problems. Have you ever considered a univ. level study of Math?
It is such a true science; and yet paradoxically, it is also a very true language.