Math for devs - Sum and Product Notation
February 12, 2023
You probably already saw the following notation:
or the following one:
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
-
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
- 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 . 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 . The upper limit is the number of times the sum or product is calculated (aka the number of iterations 👀).
- The fourth and last part is the expression. It's the expression that is summed or multiplied for each value from the lower limit () to the upper limit (). In the image above, it's generically represented as .
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.
- Let's say we want to calculate the sum of the first 15 natural numbers. We can do it like this:
The result is 120.
- 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:
The result is 2520.
- 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:
The result is 10100.
- 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:
The result is 214250.
- Our last examples is a double sum.
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: . The result is
- Then, we have to solve the leftmost sum using the result of the rightmost sum as the expression. Like this: .
Summation and product notations are just for loops 🤯
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:
We can translate this sum notation to a Typescript code like this:
let sum: number = 0;
for (let i = 1; i <= 333; i++) {
sum += 3 * i;
}
Another example, given the following product notation:
We can translate this product notation to a Typescript code like this:
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.