From 0f0696298f5037781abb7a32fafe6d0ffe513666 Mon Sep 17 00:00:00 2001 From: Vinay Reddy Date: Sat, 7 Oct 2023 21:50:47 +0530 Subject: [PATCH] Create golden_ratio.py --- golden_ratio.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 golden_ratio.py diff --git a/golden_ratio.py b/golden_ratio.py new file mode 100644 index 0000000..ecaa9ae --- /dev/null +++ b/golden_ratio.py @@ -0,0 +1,22 @@ +def golden_ratio(iterations=15): + # Start with an initial approximation of the golden ratio, typically 1. + ratio = 1 + + # Iterate to improve the approximation. + for _ in range(iterations): + # The golden ratio is defined as (1 + sqrt(5)) / 2. + # We can approximate it by adding 1 to the reciprocal of the current approximation. + # This approach iteratively refines the approximation. + ratio = 1 + 1 / ratio + + return ratio + +# Example usage: +approximations = [] +for i in range(1, 16): + approximation = golden_ratio(i) + approximations.append(approximation) + +print("Approximations of the Golden Ratio:") +for i, approx in enumerate(approximations): + print(f"Iteration {i+1}: {approx:.16f}")