Is mathematics important for programming? python example

is mathematics important for programming. Correctness proof. python example

People that want to learn (or are learning) programming, have a common question: is mathematics important for programming? In this article, I’ll answer that question through a simple python example.

The short answer to the question is yes, mathematics is important for programming. But we can also say that it is not compulsory. Let’s say that it has many benefits for programmers to be good at math.

Do you want to know why I think like that? Keep reading and you will find the answer seeing an example in action.

Solving a programming problem

Let’s say a farmer wants to start growing rabbits. The budget is tight, and he can only afford to buy a new-born male and a female. He knows that rabbits can start reproducing after a month and, at the end of the second month, the female can produce another couple of rabbits.

One assumption the farmer is making is that the rabbits won’t die, and the females always produce two more rabbits every month after they are 1 month old.

The farmer wants to know if it would make sense for him to start with his new project. So, he wants to know how many rabbits he will have in one year, 2 years or any number of year or months he decides.

The solution to the problem using mathematics

The same question was made a long time ago. A mathematician (mathematics eh?) found the answer that question: the Fibonacci sequence.

f(0)=0, f(1)=1

f(n) = f(n-1) + f(n-2), where n (n>1) is the number of months.

The implementation of such formula is easy in any programming language. Notice that the solution [f(n)] is expressed as the solution of smaller instances [f(n-1) and f(n-2)] of the same problem. This is a good hint that we can use recursion (see this article to read more about recursion).

The python implementation for this is below.

def fibonacci(n):
     if n==0:
         return 0
     elif n==1:
         return 1
     else:
         return fibonacci(n-1) + fibonacci(n-2)
 
 
 if __name__ == "__main__":
     months = int(input("Enter the number of moths: "))
     print (fibonacci(months))

Let’s see how long it takes for we to get the solution.

This is a very simple python code although inefficient and not scalable.

As you can see above, the time we have to wait to get an answer increases considerably when we increase the number of months.

It means the solution is not scalable. Scalability is a must-know programming concept. Every time you write code you should ask yourself if is scalable and if is efficient (complexity).

How can we solve this issue?

There is a topic in discrete mathematics called recurrence relations. Part of that topic teaches us how to solve Linear Homogeneous Recurrence Relations with Constant Coefficients, which is the case of the Fibonacci sequence.

What is the meaning of this? It means that we can have a (non-recurrent) formula to find out the result.

Let’s see the python implementation and the time it takes to finish the calculations.

def fib(n):
    sqr5 = math.sqrt(5)
    term1 = (1 + sqr5) / 2
    term2 = (1 - sqr5) / 2
    return int((term1 ** n - term2 ** n) / sqr5)

As you can see, there is no recursion in that code and no loops, therefore the time complexity is less than the previous solution.

This type of coding skill will make your code be above the average. It will make your code efficient and scalable.

If you want to experience scalability problems, try the first implementation (recursion) with 36 months. Then, try the second implementation with the same number of months. 

I’m sure you will fully understand the importance of scalability and efficiency once you do that test.

There are many examples like the previous one. If you just look around, you will find them very easy.

Another use of mathematics in programming

I hope you heard by now that algorithms are at the heart of computing in general, and in programming in particular.

So, let’s say you designed a new algorithm, and you want to verify or prove that the algorithm works as expected.

The more used way is to create tests (Unit test, White Box and Black Tests, etc.). The first to consider is to make that type of tests, the algorithm has to be implemented. You cannot do that type of test in an algorithm designed in a paper. On the other hand, after the implementation, if everything goes well, you only proof that your implementation works for the pairs input-output you used.

However, there is a way that we can prove that an algorithm (no need to be implemented) works for any input-output. This is particularly useful when there is an infinite number of possible inputs. 

For instance, if you are creating an algorithm to sum two numbers, how many inputs you can have? Yes, the answer is infinity.

As you might already guess, you can do these using mathematics. 

You can prove that an algorithm works (even without implementing in a programming language) by using proof methods: mathematical induction, among others.

In several situations, it is impractical to try to create a mathematic proof of an algorithm. However, if it is possible to do it, it is worthy.

If you want to see examples of how this is done, you can let me know using the contact form available from the menu.

Summary

Mathematics is very important in programming and computer science in general.

With the use of mathematics, we can create quality software, efficient and scalable.

Many companies are working very hard to create scalable systems. Just to mention a few think about Google, Amazon, Facebook. 

All of them have systems in place to guarantee efficiency and scalability. Otherwise, they couldn’t offer their services all over the world.

H@appy coding!