Math for devs - Sum and Product Notation
Tags: math, computerscience, tutorial
Published: 2023-02-22 12:08:29

You probably already saw the following notation:

{% katex %}
\sum_{i=1}^{n} i(i+1)
{% endkatex %}

or the following one:

{% katex %}
\prod_{i=1}^{n} i + 3
{% endkatex %}

Very complicated syntax, right? A bunch of strange symbols and letters that you don't understand. But, what if I told 
you that it's actually very simple? Let's see how it works.

## Presenting the symbols

![Pi and Sigma symbols](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mazl96jwfp4q2bb4tw2u.png)

- The first symbol is the sum symbol. It's a capital Greek letter called **sigma**. It's written like this: Σ and can be called as **"summation notation"**.

- The second symbol is the product symbol. It's a capital Greek letter called **pi**. It's written like this: ∏ and can be called as **"product notation"**.

## Understanding the anatomy of the notation

![Pi and Sigma notation anatomy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o27ho9cpegnjw5l1ma7m.png)

- The first part is the symbol itself. It's either the sum symbol or the product symbol (In the image above, it's the sum symbol, but the product symbol is also valid).
- The second part is the lower limit, or the starting index. It is written in the bottom part of the symbol.
It's usually written as `i = 1`. The lower limit can assume any value, but it's usually 1. If you want to start from a different number, you can change it.
- The third part is the upper limit, or the final index. It is written in the top part of the symbol. It's usually written as `n`.
The upper limit is the number of times the sum or product is calculated (aka the number of iterations :eyes:).
- The fourth and last part is the expression. It's the expression that is summed or multiplied for each value from the lower limit (`i`) to the upper limit (`n`). In the image above, it's
generically represented as `Xi`.

**OBS: The same rules apply for the ∏ (pi/product) symbol, the only difference is that, instead of summing, it multiplies the expression.**

## Examples

Now that we know the anatomy of the notation, let's see some examples.

1. Let's say we want to calculate the sum of the first 15 natural numbers. We can do it like this:

{% katex %}
\sum_{i=1}^{15} i
{% endkatex %}

The result is 120.

2. Let's say we want to calculate the product of the first 5 natural numbers, but we want to add 2 to each number before multiplying it. We can do it like this:

{% katex %}
\prod_{i=1}^{15} i + 2
{% endkatex %}

The result is 2520.

3. Now, we want to calculate the sum of each number from 1 to 100, but we want to multiply each number by 2. We can do it like this:

{% katex %}
\sum_{i=1}^{100} i \times 2
{% endkatex %}

The result is 10100.

4. We want a nested sum. We want to calculate the sum of the first 10 natural numbers, but we want to multiply each number by the sum of the first 20 natural numbers. We can do it like this:

{% katex %}
\sum_{i=1}^{10} i \times \sum_{j=1}^{20} j
{% endkatex %}

The result is 214250.

5. Our last examples is a double sum.

{% katex %}
\sum_{i=1}^{10} \sum_{j=1}^{4} i \times j
{% endkatex %}

The result is 550.
This last example is a bit more complicated, but it's still very simple. To solve this, we have to do the following:

- Begin by calculating the rightmost sum, Like this: `(i * 1) + (i * 2) + (i * 3) + (i * 4)`. The result is `10`
- Then, we have to solve the leftmost sum using the result of the rightmost sum as the expression. Like this:

{% katex %}
\sum_{i=1}^{10} 10i
{% endkatex %}

## Summation and product notations are just for loops :exploding_head:

At this point, you might have figured out that the sum and product notations are just for loops. And you're right! They are literally just for loops.
For example, considering the following sum:

{% katex %}
\sum_{i=1}^{333} 3 \times i
{% endkatex %}

We can translate this sum notation to a Typescript code like this:

```typescript
let sum: number = 0;

for (let i = 1; i <= 333; i++) {
  sum += 3 * i;
}
```

Another example, given the following product notation:

{% katex %}
\prod_{i=1}^{10} i + 2
{% endkatex %}

We can translate this product notation to a Typescript code like this:

```typescript
let product: number = 1;

for (let i = 1; i <= 10; i++) {
  product *= i + 2;
}
```

## Conclusion

In this article, we learned what sum and product notations are, how to use them, and how to translate them to for loops.