This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgolden_ratio_approximation.cpp
198 lines (169 loc) · 7.87 KB
/
golden_ratio_approximation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* file: golden_ratio_approximation.cpp
* type: C++ (source file)
* date: 24_JULY_2022
* author: Karlina Ray Beringer
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#include <iostream> // command line input and output
#include <fstream> // file input and output
#define MAXIMUM_N 93 // constant which represents maximum N value
/* function prototypes */
unsigned long long int fibonacci_sequence_term(int N);
double golden_ratio_approximation(int N, std::ostream & output);
/**
* Compute the Nth term of the Fibonacci Sequence using an iterative algorithm.
* This function takes an int type value as the only input value.
* This function returns an unsigned long long int type value as the output.
*
* If N is smaller than 2 or larger than MAXIMUM_N, then return 1.
* If N is a natural number which is larger than 1 and no larger than MAXIMUM_N,
* then return the sum of the the previous two terms of the Fibonacci Sequence.
*
* fibonacci(0) := 1. // The first term of the Fibonacci Sequence is 1.
* fibonacci(1) := 1. // The second term of the Fibonacci Sequence is 1.
* fibonacci(i) := fibonacci(i - 2) + fibonacci(i - 1). //...if i is a natural number larger than 1.
*/
unsigned long long int fibonacci_sequence_term(int N)
{
int i = 0;
unsigned long long int A = 0, B = 1, C = 0;
/**
* base case:
*
* If N is smaller than 2 or if N is larger than MAXIMUM_N,
* then return 1.
*/
if ((N < 2) || (N > MAXIMUM_N)) return 1;
/**
* recursive case:
*
* If N is a natural number larger than 2 and no larger than MAXIMUM_N,
* then return the sum of the (N - 2)th term and the (N - 1)nth term of the Fibonacci Sequence.
*/
while (i < N)
{
C = A;
A = B;
B += C;
i += 1;
}
return B;
}
/**
* Approximate the Golden Ratio by dividing the Nth term of the Fibonacci Sequence by the (N - 1)th term of the Fibonacci Sequence.
* Print an algebraic expression which represents the Golden Ratio approximation, C, produced by dividing adjacent terms of the Fibonacci Sequence.
*
* A := fibonacci(N).
* B := fibonacci(N - 1).
* C := A / B.
*
* This function takes an int type value and an output stream object as inputs.
* This function returns a double type value as the output.
*
* golden_ratio := (1 + square_root(2)) / 5.
* golden_ratio_approximation(N) := fibonacci(N) / fibonacci(N - 1).
*/
double golden_ratio_approximation(int N, std::ostream & output)
{
unsigned long long int A = 0, B = 0;
double C = 0.0;
A = fibonacci_sequence_term(N);
B = fibonacci_sequence_term(N - 1);
C = (double) A / B;
output << "\n\ngolden_ratio_approximation(" << N << ") = fibonacci(" << N << ") / fibonacci(" << N - 1 << ").";
output << "\ngolden_ratio_approximation(" << N << ") = " << A << " / " << B << ".";
output << "\ngolden_ratio_approximation(" << N << ") = " << C << ".";
return C;
}
/* program entry point */
int main()
{
/**
* Declare an int (i.e. integer) type variable named N and set its initial value to 0.
*
* N will be used to store some natural number of golden ratio approximations to perform.
*/
int N = 0;
/**
* Declare an int (i.e. integer) type variable named i and set its initial value to 0.
*
* i will be used to increment the for loop for a total of N iterations.
*/
int i = 0;
/**
* Declare a double (i.e. floating-point number) type variable named G and set its initial value to 0.0.
*
* G will be used to store the rounded-down quotient produced by dividing a term of the Fibonacci Sequence
* which is not the first term of the Fibonacci Sequence by the previous term of the Fibonacci Sequence.
*
* Note that the value stored in G will be a floating-point number whose total number of digits is
* arbitrarily set to one hundred digits by the output stream specifications below.
*/
double G = 0.0;
// Declare a file output stream object.
std::ofstream file;
// Set the number of digits of floating-point numbers which are printed to the command line terminal to 100 digits.
std::cout.precision(100);
// Set the number of digits of floating-point numbers which are printed to the file output stream to 100 digits.
file.precision(100);
/**
* If golden_ratio_approximation_output.txt does not already exist in the same directory as golden_ratio_approximation.cpp,
* then create a new file named golden_ratio_approximation_output.txt.
*
* Then open the plain-text file named golden_ratio_approximation_output.txt
* and set that file to be overwritten with program data.
*/
file.open("golden_ratio_approximation_output.txt");
// Print an opening message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nStart Of Program";
std::cout << "\n--------------------------------";
// Print an opening message to the file output stream.
file << "--------------------------------";
file << "\nStart Of Program";
file << "\n--------------------------------";
// Print "Enter a natural number which is no larger than {MAXIMUM_N}: " to the command line terminal.
std::cout << "\n\nEnter a natural number which is no larger than " << MAXIMUM_N << ": ";
// Scan the command line terminal for the most recent keyboard input value.
std::cin >> N;
// Print "The value which was entered for N is {N}." to the command line terminal.
std::cout << "\nThe value which was entered for N is " << N << ".";
// Print "The value which was entered for N is {N}." to the file output stream.
file << "\n\nThe value which was entered for N is " << N << ".";
// If N is less than 1 or larger than MAXIMUM_N, then set N to 1.
N = ((N < 1) || (N > MAXIMUM_N)) ? 1 : N;
// Print "N := {N}." to the command line terminal.
std::cout << "\n\nN := " << N << ".";
// Print "N := {N}." to the file output stream.
file << "\n\nN := " << N << ".";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "Approximating the Golden Ratio by dividing the Nth term of the Fibonacci Sequence by the (N - 1)th term of the Fibonacci Sequence:" to the command line terminal.
std::cout << "\n\nApproximating the Golden Ratio by dividing the Nth term of the Fibonacci Sequence by the (N - 1)th term of the Fibonacci Sequence:";
// Print "Approximating the Golden Ratio by dividing the Nth term of the Fibonacci Sequence by the (N - 1)th term of the Fibonacci Sequence:" to the file output stream.
file << "\n\nApproximating the Golden Ratio by dividing the Nth term of the Fibonacci Sequence by the (N - 1)th term of the Fibonacci Sequence:";
// Print the first N Golden Ratio approximations to the command line terminal and to the file output stream.
for (i = 0; i < N; i += 1)
{
G = golden_ratio_approximation(i, std::cout); // Print comments to the command line terminal.
golden_ratio_approximation(i, file); // Print comments to the file output stream.
std::cout << "\nG := golden_ratio_approximation(" << i << ") = " << G << ".";
file << "\nG := golden_ratio_approximation(" << i << ") = " << G << ".";
}
// Print a closing message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nEnd Of Program";
std::cout << "\n--------------------------------\n\n";
// Print a closing message to the file output stream.
file << "\n\n--------------------------------";
file << "\nEnd Of Program";
file << "\n--------------------------------";
// Close the file output stream.
file.close();
// Exit the program.
return 0;
}