fork download
  1. import math
  2.  
  3. def continued_fraction_sqrt2(iterations=10):
  4. x = 0
  5. for _ in range(iterations):
  6. x = 1 / (2 + x)
  7. result = 1 + x
  8. return result
  9. iterations = 10
  10. approx = continued_fraction_sqrt2(iterations)
  11. print(f"Approximation of sqrt(2) after {iterations} iterations: {approx:.15f}")
  12. print(f"math.sqrt(2) = {math.sqrt(2):.15f}")
  13. print(f"Differece = {abs(math.sqrt(2) - approx):.15f}")
Success #stdin #stdout 0.08s 14200KB
stdin
stdout
Approximation of sqrt(2) after 10 iterations: 1.500000000000000
math.sqrt(2) = 1.414213562373095
Differece = 0.085786437626905