\sum_{i=m}^{n} f(x) = f(m) + f(m+1) + \cdots + f(n) \\
sigma.py
def sigma(m, n, func, s = 0) :
if m > n: return s
return sigma(m + 1, n, func, s + func(m))
sigma_test.py
print(sigma(1, 10, lambda x : x))
55
print(sigma(1, 3, lambda x : 3 * 5 ** (x - 1)))
93
I found a suitable problem on the Web and entered it, but it seems to work properly. There may be a more efficient writing style, a more fashionable writing style, or something that has already been prepared.
Since there is a mathematical function library called NumPy, most of the formulas seem to be complete. .. ..
Introduction to Python Numerical Library NumPy http://rest-term.com/archives/2999/
Recommended Posts