Part of the series

Several example codes

~1 min read • Updated Oct 13, 2025

Program Overview

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.


Python Code:


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}")

Sample Output (input: n = 5):


Value of φ with 5 terms is: 0.618034

Written & researched by Dr. Shahin Siami