0

Remember High-school maths ? there was this greek letter Sigma ( if i remember correctly ) , followed by an algebraic function like `f(n)`

, where `n = a to b`

would be short-formed as `a`

below the `sigma`

, and `b`

sitting on top of it. I want to make a program version of that. I dont want code , just the idea how that can be done. I tried it out using scala , and it works . This is the working scala version :

```
object MySum {
/**
* Summation takes in a function f , that returns a type double
* we calculate that function with each value from a to b ,
* increasing 1 at a time
*/
def Summation(f: Double => Double, a: Double, b: Double): Double = {
// tailwise recursion
def tailwiseSummation(accumulator: Double, count: Double): Double = {
if (count > b) accumulator
else tailwiseSummation(accumulator + f(count), count + 1)
}
// accumulator starts with f(a) , so count starts from a+1 to remove off-by-one error
tailwiseSummation(f(a), a + 1)
}
def Factorial(num: Double): Double = {
def tailwise(accumulator: Double, deprecator: Double): Double = {
if (deprecator == 0) accumulator
else tailwise(accumulator * deprecator, deprecator - 1) // deprecator deprecating 1 unit at a time
}
tailwise(1, num)
}
def main(args: Array[String]) {
println(MySum.Summation(Factorial(_), 2, 7));
}
}
```