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.

Pi notation and JavaScript implementations

Also called the product notation, capital Pi notation is the mathematical shorthand to express the product of a series of numbers, where an optional function has been applied to each iteration.

Why? The motivation behind the notation

Imagine having to represent - mathematically - the product of numbers one through ten.

$$ 1 \times 2 \times 3 \times 4 \times 5 \times 6 \times 7 \times 8 \times 9 \times 10 $$
Figure 1: Numbers one through ten being multiplied.

Seems unnecessarily long, but manageable. What happens when we try to represent the product of numbers one through one hundred?

$$ 1 \times 2 \times 3 \times 4 \times 5 \times 6 \times 7 \times 8 \times 9 \times 10 \times 11 \times 12 \times 13 \times 14 \times 15 \times 16 \times 17 \times 18 \times 19 \times 20 \times 21 \times 22 \times 23 \times \cdots 100 $$
Figure 2: This representation is not very sophisticated.

It quickly gets silly and inefficient. The problem is further compounded if we wanted to express the product of a series where we apply an operation to each iteration, like adding two to each step.

$$ (1 + 2) \times (2 + 2) \times (3 + 2) \times (4 + 2) \times (5 + 2) \times (6 + 2) \times (7 + 2) \times (8 + 2) \times (9 + 2) \times \cdots (100 + 2) $$
Figure 3: This representation is unwieldy.

Completely unwieldy.

Through Pi notation, we can tersely express the product of a series of numbers, where an optional operation has been applied to each iteration. If you understand the loop construct in programming, capital Pi notation is very easy to grasp.

The notation and its parts

Here's the notation.

$$ \prod_{i=1}^{10} i $$
Figure 4: An example of pi notation.

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

  • Index of multiplication, $i$.
  • First value, $1$.
  • Last value, $10$. It sits on top of the Pi.
  • Expression, the actual operation we will apply to every iteration.

Examples with JavaScript implementation

Going back to the original example: in this expression, we are simply multiplying every number from one to ten. The multiplication is implied by the product notation itself.

$$ \prod_{i=1}^{10} i = 1 \times 2 \times 3 \times 4 \times 5 \times 6 \times 7 \times 8 \times 9 \times 10 = 3628800 $$
Figure 5: Demonstrates how concise pi notation is.

Here's a JavaScript implementation with a for loop.

In this expression, we are multiplying the square roots of every number from one to 5. The actual multiplication of each iteration is implied by the capital Pi.

$$ \begin{align} \prod_{i=1}^{5} \sqrt{i} & = \sqrt{1} \times \sqrt{2} \times \sqrt{3} \times \sqrt{4} \times \sqrt{5} \\ & \approx 10.95 \end{align} $$
Figure 6: Another example of pi notation.

Here it is implemented with a for loop.


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