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

16 lines
391 B
Python

def f(tol):
g = lambda k: 1 + 1/k**2
iterationCount = 2
result = 2
while True:
prevResult = result
result *= g(iterationCount)
if result - prevResult < tol:
return (result, iterationCount)
iterationCount += 1
if __name__ == "__main__":
result, iterationCount = f(0.01)
print(f'Produktet ble {"{:.2f}".format(result)} etter {iterationCount} iterasjoner.')