Member-only story
Math’s Sigma (Σ) Symbol Explained With JavaScript
It’s easy when you see it as code
Math’s Sigma (Σ) Symbol
In mathematics, the sigma symbol, Σ, is used to indicate that a sequence of numbers is to be added up after operating on each number with a rule.
For example, you might want to add up the numbers one through three after multiplying each by two. So, something like:
Operate on 1 through 3 by multiplying each by 2
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6Add up the results
2 + 4 + 6 = 12
This can be expressed mathematically as:
Add up a sequence from 1 to 3 after multiplying each by 2:
3
Σ 2a
i=1"a" represents the current number in the sequence
When generalized it is:
Add up a sequence from 1 to 3 after multiplying each by 2:
n
Σ 2a
i=m"a" is the current number in the sequence
"n" is the upper bound number
"m" is the lower bound number
This can be written as a function in any software language. I’ll use JavaScript to illustrate it here.
The Ground Work
First, let’s create a function to mimic the entirety of the Σ’s functionality.
function mathSumSymbol () {}
There are a lot of ways to implement this from here. I’ll demonstrate one.
Iterating Through the Lower and Upper Bounds With an Increment of 1
Let’s first handle iterating from the lower bound to the upper bound. This is something we do all the time with for
loops, so let’s use that.
It is important to mention that the upper bound is inclusive, so the index must be less than or equal to (<=) the upper bound, not less than (<).
function mathSumSymbol (lowerBound, upperBoundInclusive) {
for(let i = lowerBound; i <= upperBoundInclusive; ++i) { }
}
It is also important to note that we are incrementing by 1 (++i
). The math Σ symbol always adds 1 from the lower bound until the upper bound is…