The Sigma notation means “sum up”. Essentially, we just add together everything after the Sigma symbol.

Let’s take this:

You have *n* after the Sigma, but how to you “sum up” *n*? Well, if we take a look under the Sigma symbol we see *n=1* and if we look above the Sigma we see *5. *This means *n* becomes the range between *1* and *5*, inclusive.

So what’s the answer?

So what does that look like as code?

### Python

def sigma(n, limit): result = 0 for x in range(n, limit+1): # +1 as range in not inclusive result += x return result print(sigma(1,5)) # Gives 15

### Scala

def sigma(n:Int, limit: Int): Int = { n.to(limit).foldLeft(0)((x,y) => x + y) // foldLeft could be replaced with .sum } println(sigma(1,5)) // Gives 15

But, that’s not the full story. Consider this:

The difference? We’re performing some additional work on *n,* we’re squaring it*. *Let’s update our functions to reflect being able to perform additional work on *n*:

### Python

def sigma(n, limit, fx): result = 0 for x in range(n, limit+1): # +1 as range in not inclusive result += fx(x) return result def squareIt(x): return x**2 print(sigma(1, 5, squareIt)) # Gives 55

### Scala

def sigma(n:Int, limit: Int, fx: Int => Int): Int = { n.to(limit).foldLeft(0)((x,y) => x + fx(y)) } def squareIt(x: Int): Int = x * x println(sigma(1,5, squareIt)) // Gives 55