# Computer Algorithms

One thing I realized that many people did not know about algorithms in computer science is just how important it is, and how much the programs we use in a daily basis rely on them. Without these algorithms, computers wouldn’t even run. To start, perhaps defining “algorithm” is a good first step. When I refer to it, I’m talking not about math but of a specific pattern of writing code that achieves a goal when meeting certain criteria. That is a lot of words to say when possible, you can make things faster by being smart. But, I’d say the best way to understand this is with an example, with something called prefix sums.

## The Problem

Below is an image of 2 lists, the top one the original, and the bottom one the prefix-summed list. Pretend that we are trying to find the sum of the second to fourth numbers, so the second, third, and fourth summed.

You can see that the second list has numbers that are equal to the sum of all numbers before that location + that location.  First, the code goes to the location of the second index in the second list, which in this case is 10, representing the sum of 1, 2, 3, and 4.

Then, since we want the sum of the second, third, and forth numbers we have to subtract all numbers before it, which in this case is just the first number, 1.

Subtracting the 1 from the 10, we get 9, which if you count is the sum of the second to fourth numbers, respectively.

That is it! But this simple change makes it so that you only have to go through all of the numbers once, at the beginning when you are creating the prefix sum, and for every query you just have to subtract 2 numbers. For computers, it is quite quick to find the number if you know its position, so this subtraction can count as one operation. That means that instead of having to do 10,000 * 500,000 operations, you only have to do 10,000 + 500,000 operations (numbers from the first example), and that makes all of the difference in the world. With sufficiently large numbers, this difference is like waiting your whole life and not getting your answers, versus a few seconds.

Side note, while this is exactly how an algorithm works, it is not really an algorithm, and more of a data type, which is that prefix summed list. This is just the best and easiest way of showing what an algorithm is to those who may not be familiar with computer science/math, which is why I chose it.

Thank you for reading, and I hope you learned something!