Skip to content

Instantly share code, notes, and snippets.

@makoru-hikage
Last active October 12, 2017 11:47
Show Gist options
  • Save makoru-hikage/a9edba0c4013bb9355e2cab9d204b022 to your computer and use it in GitHub Desktop.
Save makoru-hikage/a9edba0c4013bb9355e2cab9d204b022 to your computer and use it in GitHub Desktop.
A simple implementation of summation notation in Python
"""
## Derived from here:
## https://math.stackexchange.com/questions/1694311/is-sigma-sigma-a-mathematical-way-of-doing-a-for-loop/1694322
## (c) CC-BY-SA 3.0
"""
## Definition
def sum_notation(i=0, end=1, f= lambda x : x):
return f(i) + sum_notation(i+1, end, f) if (i <= end) else 0
## Usage
y = lambda x : 1 / (2 ** x)
print (sum_notation(0, 3))
@makoru-hikage
Copy link
Author

If you use capital Sigma or capital Pi notation, use tail call recursion instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment