This repository was 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 patheulers_number_approximation.cpp
286 lines (221 loc) · 11.2 KB
/
eulers_number_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// file: eulers_number_approximation.cpp
// type: C++ (source file)
// date: 08_NOVEMBER_2022
// author: Karlina Ray Beringer (@karbytes)
// license: PUBLIC_DOMAIN
// description: The command line application which is generated by the C++ source code in this file generates an approximation of the the mathematical constant named e (i.e. Euler’s Number).
//
/* preprocessing directives */
#include <iostream> // command line input and output
#include <fstream> // file input and output
#define MAXIMUM_N 65 // constant which represents maximum N value
/* function prototypes */
unsigned long long int compute_factorial_of_N_using_iteration(int N);
long double e(int N, std::ostream & output);
/**
* Compute N factorial using an iterative algorithm.
*
* If N is a natural number, then N! is the product of exactly one instance
* of each unique natural number which is less than or equal to N.
* N! := N * (N - 1) * (N - 2) * (N - 3) * ... * 3 * 2 * 1.
*
* If N is zero, then N! is one.
* 0! := 1.
*/
unsigned long long int compute_factorial_of_N_using_iteration(int N)
{
// Declare an int type variable (i.e. a variable for storing integer values) named i.
// Set the intial value which is stored in i to zero.
int i = 0;
// Declare an unsigned long long int type variable (i.e. a variable for storing integer values) named F.
// Set the intial value which is stored in F to zero.
unsigned long long int F = 0;
// If N is larger than zero and if N is no larger than MAXIMUM_N, set i to N.
// Otherwise, set i to 0.
i = ((N > 0) && (N <= MAXIMUM_N)) ? N : 0;
// If N is larger than zero, set F to N.
// Otherwise, set F to 1.
F = (N > 0) ? N : 1;
// While i is larger than zero:
while (i > 0)
{
// If i is larger than 1, multiply F by (i - 1).
if (i > 1) F *= i - 1;
// Decrement i by 1.
i -= 1;
}
// Return the value stored in F (i.e. factorial N (i.e. N!)).
return F;
}
/**
* Generate an approximation of the the mathematical constant named e (i.e. Euler’s Number).
*
* The value returned by this function is a floating-point number value.
*/
long double e(int N, std::ostream & output)
{
// Declare a long double type variable (i.e. a variable for storing floating-point number values) named A.
// Set the intial value which is stored in A to one.
long double A = 1.0;
// Declare an int type variable (i.e. a variable for storing integer values) named i.
// Set the intial value which is stored in i to zero.
int i = 0;
// Declare a pointer to an unsigned long long int type variable named T.
unsigned long long int * T;
// If N is smaller than zero or if N is larger than MAXIMUM_N, set N to one.
N = ((N < 0) || (N > MAXIMUM_N)) ? 1 : N;
// Allocate N contiguous unsigned long long int sized chunks of memory to an array for storing N floating-point values.
// Store the memory address of the first element of that array in T.
T = new unsigned long long int [N];
// Print "number_of_bytes(unsigned long long int) := {sizeof(unsigned long long int)}." to the output stream.
output << "\n\nnumber_of_bytes(unsigned long long int) := " << sizeof(unsigned long long int) << ".";
// Print "memory_address_of(T) = (&T) := {memory_address_of(T)}." to the output stream.
output << "\n\nmemory_address_of(T) = (&T) := " << &T << ".// & is a reference operator";
// Print "T := {T}." to the output stream.
output << "\n\nT := " << T << ". // pointer to unsigned long long int type variable";
// Print "(*T) := {*T}." to the output stream.
output << "\n\n(*T) := " << (*T) << ". // dereferenced pointer to unsigned long long int type variable \n";
// For each integer value represented by i starting at 0 and ending at N in and in ascending order:
// print the memory address of the ith element of the unsigned long long int type array represented by T to the output stream.
for (i = 0; i < N; i += 1)
{
// Print "(&T[{i}]) := {memory_address_of(T[i])}." to the output stream.
output << "\n(&T[" << i << "]) := " << &T[i] << ". // memory address of T[" << i << "]";
}
// Print a newline character to the output stream.
output << '\n';
// For each integer value represented by i starting at 0 and ending at N in and in ascending order:
// set value of the ith element of the unsigned long long int type array represented by T to (N - i) and
// print the data value which is stored in the ith element of the array to the output stream.
for (i = 0; i < N; i += 1)
{
// Store the result of the arithmetic expression (N - i) in T[i].
T[i] = N - i;
// Print "T[{i}] := {T[i]}." to the output stream.
output << "\nT[" << i << "] := " << T[i] << " = (" << N << " - " << i << ").";
}
// Print a newline character to the output stream.
output << '\n';
// For each integer value represented by i starting at 0 and ending at N in and in ascending order:
// set value of the ith element of the unsigned long long int type array represented by T to (N - i)! and
// print the data value which is stored in the ith element of the array to the output stream.
for (i = 0; i < N; i += 1)
{
// Print "T[{i}] := factorial({T[i]}) = ({T[i]})! = " to the output stream.
output << "\nT[" << i << "] := factorial(" << T[i] << ") = (" << T[i] << ")! = ";
// Store (N - i)! in T[i].
T[i] = compute_factorial_of_N_using_iteration(T[i]);
// Print {T[i]} to the output stream.
output << T[i];
}
// Print a newline character to the output stream.
output << '\n';
// For each integer value represented by i starting at 0 and ending at N in and in ascending order:
// print the value of (1 / (N - i)!) to the output stream.
for (i = 0; i < N; i += 1) output << "\n(1 / T[" << i << "]) = (1 / " << T[i] << ") = " << (long double) 1 / T[i] << ".";
// For each integer value represented by i starting at 0 and ending at N in and in ascending order:
// add the value of (1 / (N - i)!) to A and print the contents of A to the output stream.
for (i = 0; i < N; i += 1)
{
output << "\n\nA := A + (1 / (" << N << " - " << i << ")!)";
output << "\n = " << A << " + (1 / " << T[i] << ")";
output << "\n = " << A << " + " << (long double) 1 / T[i];
A += (long double) 1 / T[i];
output << "\n = " << A << ".";
}
// De-allocate memory which was assigned to the array named T.
delete [] T;
// Return the value which is stored in A.
return A;
}
/* program entry point */
int main()
{
// Declare a file output stream object named file.
std::ofstream file;
// Declare an int type variable (i.e. a variable for storing integer values) named N.
// Set the intial value which is stored in N to one.
int N = 1;
// Declare a long double type variable (i.e. a variable for storing floating-point number values) named A.
// Set the intial value which is stored in A to one.
long double A = 1.0;
// 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 the file named eulers_number_approximation_output.txt does not already exist inside of the same
* file directory as does the file named eulers_number_approximation.cpp,
* create a new file named eulers_number_approximation_output.txt.
*
* Open the plain-text file named eulers_number_approximation_output.txt
* and set that file to be overwritten with program data.
*/
file.open("eulers_number_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 << ": ";
// Print "Enter a natural number which is no larger than {MAXIMUM_N}: " to the file output stream.
file << "\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 {N} to the command line terminal.
std::cout << N;
// Print {N} to the file output stream.
file << N;
// If N is smaller than zero or if N is larger than MAXIMUM_N, set N to one.
N = ((N < 0) || (N > MAXIMUM_N)) ? 1 : N;
// Print "N := {N}." to the command line terminal.
std::cout << "\n\nN := " << N << ". // int type variable";
// Print "N := {N}." to the file output stream.
file << "\n\nN := " << N << ". // int type variable";
// Print "number_of_bytes(int) := {sizeof(int)}." to the command line terminal.
std::cout << "\n\nnumber_of_bytes(int) := " << sizeof(int) << ".";
// Print "number_of_bytes(int) := {sizeof(int)}." to the file output stream.
file << "\n\nnumber_of_bytes(int) := " << sizeof(int) << ".";
// Print "memory_address_of(A) := (&N) = {memory_address_of(A)}." to the command line terminal.
std::cout << "\n\nmemory_address_of(N) = (&N) := " << &N << ". // & is a reference operator";
// Print "memory_address_of(N) = (&N) := {N}." to the file output stream.
file << "\n\nmemory_address_of(N) = (&N) := " << &N << ". // & is a reference operator";
// Print "number_of_bytes(long double) := {sizeof(long double)}." to the command line terminal.
std::cout << "\n\nnumber_of_bytes(long double) := " << sizeof(long double) << ".";
// Print "number_of_bytes(long double) := {sizeof(long double)}." to the file output stream.
file << "\n\nnumber_of_bytes(long double) := " << sizeof(long double) << ".";
// Print "A := {A}." to the command line terminal.
std::cout << "\n\nA := " << A << ". // long double type variable";
// Print "A := {A}." to the file output stream.
file << "\n\nA := " << A << ". // long double type variable";
// Print "memory_address_of(A) = (&A) := {memory_address_of(A)}." to the command line terminal.
std::cout << "\n\nmemory_address_of(A) = (&A) := " << &A << ". // & is a reference operator";
// Print "memory_address_of(A) = (&A) := {A}." to the file output stream.
file << "\n\nmemory_address_of(N) = (&A) := " << &A << ". // & is a reference operator";
// Compute the Nth approximation of Euler's Number and store the result in A.
// Print the steps involved in generating an approximation of Euler's Number to the command line terminal.
A = e(N, std::cout);
// Print the steps involved in generating an approximation of Euler's Number to the file output stream.
e(N, file);
// Print "A = e(N) := {e(N)}." to the command line terminal.
std::cout << "\n\nA = e(N) := " << A << ".";
// Print "A = e(N) := {e(N)}." to the file output stream.
file << "\n\nA = e(N) := " << A << ".";
// 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;
}