Importance of Mathematics in Programming

Hariom Joshi
2 min readJul 17, 2022

In this monthly newsletter I share some of my learnings from the current month.

Use of Mathematics in Data Structures and Algorithms

Many people undermine the importance of Mathematics in Data Structures and algorithms, however mathematics can never be ignored when it comes to logical thinking and solving problems.

There are many problems which seems impossible to solve using logical thinking but with just a pinch of Mathematics, you can do the same problem easily. One of the example of such a problem is given below -

Given a number N, print all the substring of a number without any conversion.

Input: N = 12345

Output: Possible Substrings: {1, 12, 123, 1234, 12345, 2, 23, 234, 2345, 3, 34, 345, 4, 45, 5}

A possible answer here is with the help of recursion, but it will increase the space complexity of our code, so we can do this easily with the help of Math.log10(); function in java as follows.

Code to solve the above problem using log function

Here, as you can see just a simple log function not only reduced the time and space complexity of code, but also made the code look much more cleaner. This is just one example of the use of Mathematics in DSA, you my see numerous such examples in your problem solving once you get the gist of it.

--

--