# Why study Algorithms and how Mathematics can make a difference.

Yes, I know you can write computer code. You can use libraries to achieve just anything . But in some situations, libraries will not come to your rescue. Why bros? There is no one answer to this, but a day is coming when:

1) You will be faced with a challenge that’s unique to your organization. Maybe when this happens, you are the new Head of Engineering in your organization. Boss! You’ve got to deliver, you can’t afford to disappoint these boys looking up to you. Stackoverflow won’t help in this case.

2) You will be placed on the team developing a mission critical software. Man, after clicking that button, your program can’t afford to respond to a query after 30 secs…

3) You might want to work with a company like Google, Facebook, and you’ve got to prove that you are smarter than every other person applying to the same position. I’m sure you know you won’t be using external libraries like you normally do…

Well, Minister of Algorithms, you make noise too much. Do less talk and show us something. Okay!!! Let’s see how little things can make big difference.

EXAMPLE

Let’s checkout this question on Project Euler….

You can always checkout the site later, finish reading this, here is the question.

This question is so simple that the difficulty level according to project Euler is 5%. I am sure you can remember that Algorithm definition that says that…. it is a step by step solution to a problem. Yes, it’s correct. So you say, these are the steps required to solve this simple problem.

- Find the sum of the first 100 natural numbers
- Square the result
- Find the sum of squares of the first 100 natural numbers
- Find the difference between step 2 and step 3 … i.e step(2)-step(3).

Translating the steps above into code(Which you always love to write). You might have the following functions (Javascript ):

`function sumOfNaturalNumbers(`**n**) {

var **sum** = 0;

for (var **i** = 1; **i** <= n; **i**++) {

**sum** += **i**;

}

return **sum**;

}

The above function finds the sum of first N natural numbers.

`function squareSumOfNaturalNumbers(`**n**){

var **sum** = 0;

for (var **i** = 1; **i** <= n; **i**++) {

**sum** += **i*****i**;

}

return **sum**;

}

The above function calculates the sum of Square of first N natural numbers.

After this, you do:

varsum= sumOfNaturalNumbers(100)

varsumSquare=sum*sum

console.log(sumSquare)Answer =25502500

Also, you do the following next:

varsquareSum= squareSumOfNaturalNumbers(100)

console.log(squareSum)Answer =338350

The final answer is : `squareSum `

**— **sumSquare = **25164150**

Yes, you are correct. But wait, you did 200 iterations? What if you are to calculate for the first 1000000? you will have to iterate 2000000 times?

So you told a senior colleague at work about it. Because of his experience, he told you could have reduced the no of iterations by **half** by doing the following instead:

function calculateSums(n){

varsum= 0;

varsquareSum= 0;

for (vari= 1;i<=n;i++) {

sum+=i;

squareSum+=i*i;

}

return {sumSquare:sum*sum, sqaureSum:squareSum};

}varpartialResult= calculateSums(100);

console.log(partialResult.sumSquare -partialResult.sqaureSum)Answer =25164150

Now you only get to iterate N times…. even if N is 1Billion. Now your program is working fast… maybe not that first, after all, computers can do billions of operations in seconds. So this is not noticeable.

Errrm. so you went for an interview one day, and this question was asked. Because you have practiced it before you completed it before every other person. But the interviewer studied Computer Science with Mathematics(like me, no offense to Computer Engineers cracking codes). He told you: hey man, nice solution, your solution is okay but not as good as we would have love it to be. Because of his background in Mathematics, he knows that the task at hand has some patterns in it and anytime there is a pattern in mathematics, he’s sure there is a mathematical function that will give you the desired result.

of course, you are at an interview, so you humbled yourself and politely asked him to tell you how to do this.

It turns out that there is a mathematical function for calculating the sum of first **N** natural numbers:

` sum(n) = (n(n+1))/2`

and another for calculating the sum of Squares of first **N **natural numbers:

` squareSum(n) = (n(n+1)(2n+1))/6`

You can confirm this here at Brilliant.org: So you can write two functions as follows:

functiongetSumSquare(n) {

returnMath.pow((n * (n + 1)) / 2, 2);

}functiongetSquareSum(n) {

return(n *(n+1) * (2*n+1))/6;

}console.log(getSumSquare(100) -getSquareSum(100))Answer =25164150

Mehn, that code is sweet, simple and requires few steps to get the job done. if n is 1000000, the answer will be logged on console in few milliseconds.

Looking at this example, we moved from a *Bicycle* solution , which will definitely get to destination but might take time, to a *Motor Vehicle* solution and then to a “*Jet-like*” solution.

This is why you need to study Algorithms, to always try to find that “jet-like” solution that will make programming more fun and interesting. This is the essence of this community. We know that we do not know these stuffs, but we believe with constant thinking and research, we keep getting better day by day.

If for any reason, this article is interesting to you, please follow this publication to receive notification on future articles. If for some reasons, you don’t like it that much, still follow us….Remember, the goal is to get better day by day.

May the forces of Algorithms be with you!