def fold2(op, term, a, next, b, base):
if a > b:
return base
else:
return op (term(a), fold2(op, term, next(a), next, b, base))
def geometric_series(a, r, n):
return fold2(lambda x,y: x+y, lambda x: a*(r**(n-1)), 1, lambda n: n+1, n, a)
Can someone help me with this code. It's suppose to give a geometric series but i am only allowed to change the part def geometric_series(a, r, n). What's wrong with it?
a) geometric_series(1/2, 1/2, 3) = 0.875 (correct)
b) geometric_series(1, 2, 4) = 33 (wrong) correct answer is 15