TDT4109/Exercise 5/7 - Multiplikasjon/b.py

14 lines
422 B
Python

def f(tol, count=0):
g = lambda x: 1 + 1/x**2
product = g(count+1)
# Base case
if product < 1 + tol:
return (product, count)
newProd, newCount = f(tol, count + 1)
return (product * newProd, newCount) # Nest in the last count value without modification
if __name__ == "__main__":
result, recursionCount = f(0.01)
print(f'Rekursjonsdybden er {recursionCount}\nProduktet ble {"{:.2f}".format(result)}')