This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactorial.cpp
192 lines (156 loc) · 8.2 KB
/
factorial.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
/**
* file: factorial.cpp
* type: C++ (source file)
* date: 14_JUNE_2023
* author: karbytes
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#include <iostream> // library for defining objects which handle command line input and command line output
#include <fstream> // library for defining objects which handle file input and file output
#define MAXIMUM_N 12 // constant which represents maximum N value
/* function prototypes */
int compute_factorial_of_N_using_recursion(int N, std::ostream & output);
int compute_factorial_of_N_using_iteration(int N, std::ostream & output);
/**
* Compute N factorial (N!) using a recursive algorithm.
*
* Assume that N is an integer value and that output is an output stream object.
*
* For each compute_factorial_of_N_using_recursion function call,
* print an algebraic expression which represents N factorial.
*
* 0! := 1. // base case: when N is smaller than 1 or when N is larger than MAXIMUM_N.
* N! := N * (N - 1)! // recursive case: when N is larger than or equal to 1 and when N is smaller than or equal to MAXIMUM_N.
*/
int compute_factorial_of_N_using_recursion(int N, std::ostream & output)
{
// base case: if N is smaler than 1 or if N is larger than MAXIMUM_N, return 1.
if ((N < 1) || (N > MAXIMUM_N))
{
output << "\n\nfactorial(" << N << ") = 1. // base case";
return 1;
}
// recursive case: if N is larger than or equal to 1 and if N is smaller than or equal to MAXIMUM_N, return N multiplied by (N - 1) factorial.
else
{
output << "\n\nfactorial(" << N << ") = " << N << " * factorial(" << N - 1 << "). // recursive case" ;
return N * compute_factorial_of_N_using_recursion(N - 1, output);
}
}
/**
* Compute N factorial using an iterative algorithm.
*
* Assume that N is an integer value and that output is an output stream object.
*
* For each while loop iteration, i, print the ith multiplicative term of N factorial.
*
* ---------------------------------------------------------------------------------------------------------------------------------------------------------------
* If N is a larger than or equal to 1 and if N is smaller than or equal to MAXIMUM_N,
* N! is the product of exactly one instance of each unique natural number which is smaller than or equal to N.
*
* N! := N * (N - 1) * (N - 2) * (N - 3) * ... * 3 * 2 * 1. // if N is an arbitrarily large natural number (which, in this line, is equal to or larger than 7)
* ---------------------------------------------------------------------------------------------------------------------------------------------------------------
*
* If N is zero, then N! is one.
*
* 0! := 1.
* ---------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
int compute_factorial_of_N_using_iteration(int N, std::ostream & output)
{
int i = 0, F = 0;
i = ((N > 0) && (N <= MAXIMUM_N)) ? N : 0;
F = (N > 0) ? N : 1;
output << "\n\nfactorial(" << i << ") = ";
while (i > 0) // Execute the code block encapsulated by the while loop while the condition "i > 0" is true.
{
output << i << " * "; // Print the value of i followed by " * " to the output stream.
if (i > 1) F *= i - 1; // If i is larger than 1, multiply F by (i - 1).
i -= 1; // Decrement i by 1.
}
output << "1.";
return F;
}
/* program entry point */
int main()
{
// Declare three int type variables and set each of their initial values to 0.
int N = 0, A = 0, B = 0;
// Declare a file output stream object.
std::ofstream file;
/**
* If factorial_output.txt does not already exist in the same directory as factorial.cpp,
* create a new file named factorial_output.txt.
*
* Open the plain-text file named factorial_output.txt
* and set that file to be overwritten with program data.
*/
file.open("factorial_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 nonnegative integer which is no larger than {MAXIMUM_N}: " to the command line terminal.
std::cout << "\n\nEnter a nonnegative integer 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 smaller than 0 or if N is larger than MAXIMUM_N, set N to 0.
N = ((N < 0) || (N > MAXIMUM_N)) ? 0 : N; // A tertiary operation (using the tertiary operator (?)) is an alternative to using if-else statements.
// 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 "Computing factorial N using recursion:" to the command line terminal.
std::cout << "\n\nComputing factorial N using recursion:";
// Print "Computing factorial N using recursion:" to the file output stream.
file << "\n\nComputing factorial N using recursion:";
// Compute N factorial using recursion, store the result in A, and print each function call in the recursive function call chain to the command line terminal.
A = compute_factorial_of_N_using_recursion(N, std::cout);
// Compute N factorial using recursion and print each function call in the recursive function call chain to the file output stream.
compute_factorial_of_N_using_recursion(N, file);
// Print the value of A to the command line terminal.
std::cout << "\n\nA = factorial(" << N << ") = " << A << ". // " << N << "! = " << A << ".";
// Print the value of A to the file output stream.
file << "\n\nA = factorial(" << N << ") = " << A << ". // " << N << "! = " << A << ".";
// 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 "Computing factorial N using iteration:" to the command line terminal.
std::cout << "\n\nComputing factorial N using iteration:";
// Print "Computing factorial N using iteration:" to the file output stream.
file << "\n\nComputing factorial N using iteration:";
// Compute N factorial using iteration and print each multiplicative term of N! to the command line terminal.
B = compute_factorial_of_N_using_iteration(N, std::cout);
// Compute N factorial using iteration and print each multiplicative term of N! to the file output stream.
compute_factorial_of_N_using_iteration(N, file);
// Print the value of B to the command line terminal.
std::cout << "\n\nB = factorial(" << N << ") = " << B << ". // " << N << "! = " << B << ".";
// Print the value of B to the file output stream.
file << "\n\nB = factorial(" << N << ") = " << B << ". // " << N << "! = " << B << ".";
// 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;
}