Jorge Valle

Jorge Valle

Jorge Valle is a front end developer with a particular passion for, and expertise in, JavaScript and user interfaces. Lately, he's also been diving into machine learning.

Sigma notation and JavaScript implementations

Also called the summation notation, capital Sigma notation is the mathematical way of expressing the sum of a function iterated over a variable.

The notation and its parts

The Sigma notation can be a bit confusing or intimidating at first, but it's actually really simple. It's a short way of expressing a formula applied iteratively to a range of numbers.

If you understand the loop construct in programming, Sigma notation is very easy to grasp.

Here's the notation.

$$ \sum_{i=1}^{10} i $$
Figure 1: An example of sigma notation.

It's best explained broken down into the different components. In the example above, there's the:

  • Index of summation, $i$.
  • First value, $1$.
  • Last value, $10$. It sits on top of the Sigma.
  • Formula, the actual operation we will apply to every iteration. In this case we are just adding, which is implict through the Sigma notation itself.

Examples with JavaScript implementation

In this expression, we are adding up every number from one to ten.

$$ \sum_{i=1}^{10} i = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55 $$
Figure 2: Sigma notation, used to represent numbers one through ten being added.

Here's a JavaScript implementation with a for loop.

In this expression, we are adding up the squares of every number from one to ten. The actual addition of each iteration is implied by the Sigma.

$$ \begin{align} \sum_{i=1}^{10} i^2 & = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 \\ & = 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 \\ & = 385 \end{align} $$
Figure 3: A more complex example of sigma notation.

Here it is implemented with a while loop.

Here I have changed the index of summation from an i to an r, just to make it clear that the letter used is irrelevant.

I have retained the first value to be one, but have changed the last value to be five. Finally, the formula is a bit more complex, as it includes addition and multiplication.

$$ \begin{align} \sum_{r=1}^{5} r(r+3) & = 1(1 + 3) + 2(2 + 3) + 3(3 + 3) + 4(4 + 3) + 5(5 + 3) \\ & = 4 + 10 + 18 + 28 + 40 \\ & = 100 \end{align} $$
Figure 4: Another example of sigma notation.

Books, patrons and coffee in Caffè San Marco, Trieste