Math and Programming

◀ Input Output Redirection▶ Where Are Your Eyes?
Amazon Every experienced programmer will soon realize that programming is closely related to mathematics. A programmer good at math programs a lot more comfortably and efficiently. There are millions of programs that demonstrate this point.

Here is a simple example: the user enters a positive integer, n, and the program outputs the sum of 1, 2, 3,…, n. One way to write it is to calculate the sum the brute-force way: use a loop to add the numbers up and output that number. The bigger the number entered by the user, the longer it takes for the program to output the result.

However, we can use a formula to achieve the same end: We simply calculate (1+n)*n/2 and output the result.

This is a much better way because it always takes constant time, instead of linear time, to compute the result. A sophisticated knowledge in mathematics and flexibility in programming help a programmer write efficient programs.

In fact, a lot of numerical work is done in C++ standard library. You can see many examples in C++’s math library source code, where functions like pow(), cos(), sin(), log() are implemented.

The standard library also provides dozens of algorithms, presented in the algorithm header file. These algorithms include sort(), merge(), replace(), and many others, all of which use a lot of math.

To conclude, building a strong background in math will help you design your programs more efficiently and creatively.
Next let’s look at the BIGGEST source of bugs! Once you know it and avoid it you’ll be able to eliminate more than 90% of the bugs!
◀ Input Output Redirection▶ Where Are Your Eyes?

Questions? Let me know!