In this guide, we will explore how to Calculate the Golden Ratio Sequence in C++ along with its methodology, a demonstration, time complexity, and space complexity.
Golden Ratio:
The Golden Ratio (ϕ), also referred to as the Divine Proportion, is an irrational value close to 1.6180339887. This mathematical constant originates from the quadratic equation:
Thus, it is recommended to maintain a ratio of around 1.618 between the larger quantity (A) and the smaller quantity (B). A and B are considered to be in the golden ratio if the resulting ratio closely approximates ϕ.
This unique ratio is a crucial mathematical value and can be observed in Fibonacci progressions, organic spirals, and traditional patterns.
Instances of this proportion are present in various fields such as finance, art, architectural design, geometry, and the natural world. Its visually appealing characteristics have captivated mathematicians and artists alike for many generations.
Example:
Let's consider a scenario to demonstrate the Golden Ratio Sequence in C++.
#include <iostream>
#include <iomanip>
using namespace std;
// Function to compute the Golden Ratio sequence using Fibonacci numbers
void computeGoldenRatioSequence(int terms)
{
// Initializing the first two Fibonacci numbers
double prev = 1, curr = 1, next;
// Displaying the header
cout << "Golden Ratio Sequence using Fibonacci Numbers:" << endl;
cout << "---------------------------------------------------" << endl;
cout << setw(5) << "n" << setw(15) << "Fibonacci(n)" << setw(20) << "Golden Ratio (ϕ)" << endl;
cout << "---------------------------------------------------" << endl;
// Printing the first two Fibonacci numbers (no ratio for the first term)
cout << setw(5) << "1" << setw(15) << prev << setw(20) << "N/A" << endl;
cout << setw(5) << "2" << setw(15) << curr << setw(20) << "N/A" << endl;
// Compute and display the Golden Ratio sequence for the remaining terms
for (int q = 3; q <= terms; q++)
{
next = prev + curr; // Compute the next Fibonacci number
// Compute and display the Golden Ratio (current term / previous term)
double goldenRatio = next / curr;
cout << setw(5) << q << setw(15) << next << setw(20) << fixed << setprecision(6) << goldenRatio << endl;
// Update values for the next iteration
prev = curr;
curr = next;
}
cout << "---------------------------------------------------" << endl;
cout << "As n increases, the Golden Ratio converges to ≈ 1.618." << endl;
}
// Main function to take input from the user and compute the sequence
int main()
{
int numTerms;
// Taking input for several terms in the sequence
cout << "Enter the number of terms (minimum 2): ";
cin >> numTerms;
// Validate user input
if (numTerms < 2)
{
cout << "Error: Please enter a value greater than or equal to 2." << endl;
return 1;
}
// Call function to compute the Golden Ratio Sequence
computeGoldenRatioSequence(numTerms);
return 0;
}
Output:
Enter the number of terms (minimum 2): 10
Golden Ratio Sequence using Fibonacci Numbers:
---------------------------------------------------
n Fibonacci(n) Golden Ratio (ϕ)
---------------------------------------------------
1 1 N/A
2 1 N/A
3 2 2.000000
4 3.000000 1.500000
5 5.000000 1.666667
6 8.000000 1.600000
7 13.000000 1.625000
8 21.000000 1.615385
9 34.000000 1.619048
10 55.000000 1.617647
---------------------------------------------------
As n increases, the Golden Ratio converges to ≈ 1.618.
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Explanation:
The provided C++ code utilizes Fibonacci numbers to compute the Golden Ratio Sequence. The initial two Fibonacci numbers (prev = 1, curr = 1) are set up, and as their ratio is indeterminate, they are displayed initially. Subsequently, the program computes each Fibonacci number by adding the two preceding numbers together (next = prev + curr) while moving through the range from 3 to n. The approximation of the Golden Ratio is achieved by determining the ratio of the current Fibonacci number to the preceding one (next / curr). The program uses setw for formatting and setprecision(6) for accurate decimal representation within the formatted table for showcasing the outcomes. Ultimately, it validates the input to ensure that the user inputs a number that is not less than 2.
Applications of the Golden Ratio:
Some of the applications are listed below.
- Stock Market Analysis: Technical analysis frequently employs Fibonacci retracement levels, which are derived from ϕ (1.618), to predict probable levels of support and resistance in market movements.
- Music Theory: The ϕ-based structures are used in many musical compositions and rhythms, which improve harmony and create naturally pleasing arrangements.
- Physics and Cosmology: In physics and cosmology, the Golden Ratio explains spiral galaxy formations, black hole dynamics, and quantum mechanics. It also influences basic natural patterns.
- Architecture and Design: The ϕ-based proportions are used in architecture and design to create symmetry and visual balance in well-known buildings like the Parthenon and the Pyramids of Giza.
- Art and Aesthetics: In order to achieve the perfect composition in works of art like the Mona Lisa, famous artists like Leonardo da Vinci employed the Golden Ratio.