This Python program reads an integer n and computes the result of the following continued fraction:
φ = 1 / (1 + 1 / (1 + 1 / (1 + ... + 1))) with n nested terms
It uses a reverse iterative loop to calculate the value from the innermost fraction outward.
def compute_phi(n: int) -> float:
result = 1.0
for _ in range(n - 1):
result = 1 / (1 + result)
return result
# Read input from user
n = int(input("Enter value for n: "))
phi = compute_phi(n)
print(f"Value of φ with {n} terms is: {phi:.6f}")
Value of φ with 5 terms is: 0.618034