This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PRIME_FACTORIZATION.html
226 lines (180 loc) · 14.8 KB
/
PRIME_FACTORIZATION.html
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
<hr>
<p><strong>PRIME_FACTORIZATION</strong></p>
<hr>
<p><span style="background:#ffff00">The <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/C%2B%2B" target="_blank" rel="noopener">C++</a> program featured in this tutorial web page factorizes some integer, N, into its constituent prime number multiplicative terms (if N is prime number factorizable). Results are printed to the command line terminal interface and to an output text file. Users can input multiple values for N by either choosing to enter another N value or else exiting the program after each value for N is entered.</span></p>
<p>A <strong>prime number</strong> is a natural number which is larger than or equal to 2 and which is the multiplicative product of only itself and 1.</p>
<p>A <strong>composite number</strong> is a natural number which is larger than 1 and which is the multiplicative product of two or more prime numbers.</p>
<p><em>To view hidden text inside each of the preformatted text boxes below, scroll horizontally.</em></p>
<hr>
<p><strong>SOFTWARE_APPLICATION_COMPONENTS</strong></p>
<hr>
<p>C++_source_file: <a style="background:#000000;color:#00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.cpp</a></p>
<p>plain-text_file: <a style="background:#000000;color:#ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt</a></p>
<hr>
<p><strong>PROGRAM_COMPILATION_AND_EXECUTION</strong></p>
<hr>
<p>STEP_0: Copy and paste the C++ <a style="background:#000000;color:#00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.cpp" target="_blank" rel="noopener">source code</a> into a new text editor document and save that document as the following file name:</p>
<pre>prime_factorization.cpp</pre>
<p>STEP_1: Open a <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Unix" target="_blank" rel="noopener">Unix</a> command line terminal application and set the current directory to wherever the C++ is located on the local machine (e.g. Desktop).</p>
<pre>cd Desktop</pre>
<p>STEP_2: Compile the C++ file into machine-executable instructions (i.e. object file) and then into an executable piece of software named <strong>app</strong> using the following command:</p>
<pre>g++ prime_factorization.cpp -o app</pre>
<p>STEP_3: If the program compilation command does not work, then use the following commands (in top-down order) to install the C/C++ compiler (which is part of the <a style="background: #ff9000;color: #000000" href="https://en.wikipedia.org/wiki/GNU_Compiler_Collection" target="_blank" rel="noopener">GNU Compiler Collection (GCC)</a>):</p>
<pre>sudo apt install build-essential</pre>
<pre>sudo apt-get install g++</pre>
<p>STEP_4: After running the <strong>g++</strong> command, run the executable file using the following command:</p>
<pre>./app</pre>
<p>STEP_5: Once the application is running, the following prompt will appear:</p>
<pre>Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms: </pre>
<p>STEP_6: Enter a value for N using the keyboard.</p>
<p>STEP_7: Observe program results on the command line terminal and in the <a style="background:#000000;color:#ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt" target="_blank" rel="noopener">output file</a>.</p>
<hr>
<p><strong>PROGRAM_SOURCE_CODE</strong></p>
<hr>
<p>The text in the preformatted text box below appears on this web page (while rendered correctly by the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/HTML" target="_blank" rel="noopener">web browser</a>) to be identical to the content of the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/C%2B%2B" target="_blank" rel="noopener">C++</a> source code file whose <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/URL" target="_blank" rel="noopener">Uniform Resource Locator</a> is displayed in the green <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Hyperlink" target="_blank" rel="noopener">hyperlink</a> below. A <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Computer" target="_blank" rel="noopener">computer</a> interprets that C++ source code as a series of programmatic instructions (i.e. <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Software" target="_blank" rel="noopener">software</a>) which govern how the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Computer_hardware" target="_blank" rel="noopener">hardware</a> of that computer behaves.</p>
<p><em>(Note that angle brackets which resemble <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/HTML" target="_blank" rel="noopener">HTML</a> tags (i.e. an “is less than” symbol (i.e. ‘<‘) followed by an “is greater than” symbol (i.e. ‘>’) displayed in the aforementioned text box have been replaced (at the source code level of this web page) with Unicode symbols <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Less-than_sign" target="_blank" rel="noopener">U+003C</a> (which is rendered by the web browser as ‘<‘) and <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Greater-than_sign" target="_blank" rel="noopener">U+003E</a> (which is rendered by the web browser as ‘>’). That is because the <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/WordPress.com" target="_blank" rel="noopener">WordPress</a> web page editor interprets a plain-text versions of an “is less than” symbol followed by an “is greater than” symbol as being an opening HTML tag (which means that the WordPress web page editor deletes the content between those (plain-text) inequality symbols)).</em></p>
<p>C++_source_file: <a style="background:#000000;color:#00ff00" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.cpp" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.cpp</a></p>
<hr>
<pre>/**
* file: prime_factorization.cpp
* type: C++ (source file)
* date: 21_SEPTEMBER_2024
* author: karbytes
* license: PUBLIC_DOMAIN
*/
/** preprocessing directives */
#include <iostream> // standard input (std::cin), standard output (std::cout)
#include <fstream> // output file creation, output file overwriting, output file open, output file close
#define MAXIMUM_N 10000 // constant which represents the maximum value for N
/** function prototype */
void print_prime_factorization(int N, std::ostream& output);
/** program entry point */
int main()
{
// Declare and initialize two int type variables.
int N = 0, input_additional_values = 1;
// Declare a file output stream handler.
std::ofstream file;
/**
* If the file named prime_factorization_output.txt does not already exist
* inside of the same file directory as the file named sort_compare.cpp,
* create a new file named prime_factorization_output.txt in that directory.
*
* Open the plain-text file named prime_factorization_output.txt
* and set that file to be overwritten with program data.
*/
file.open("prime_factorization_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--------------------------------";
while (input_additional_values != 0)
{
// Prompt the user to enter an input value for N.
std::cout << "\n\nEnter a nonnegative integer value to store in the variable named N which is no larger than " << MAXIMUM_N << " to factor into its constituent prime number multiplicative terms: ";
file << "\n\nEnter a nonnegative integer value to store in the variable named N which is no larger than " << MAXIMUM_N << " to factor into its constituent prime number multiplicative terms: ";
// Scan the command line terminal for the most recent keyboard input value. Store that value in N.
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 10.
N = ((N < 0) || (N > MAXIMUM_N)) ? 10 : N;
// Print the prime number factorization for N to the command line terminal.
print_prime_factorization(N, std::cout);
// Print the prime number factorization for N to the file output stream.
print_prime_factorization(N, file);
// Ask the user whether or not to continue inputing values.
std::cout << "\n\nWould you like to continue inputting program values? (Enter 1 if YES. Enter 0 if NO): ";
// Scan the command line terminal for the most recent keyboard input value.
std::cin >> input_additional_values;
}
// 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;
}
/**
* Use an iterative method to generate each of the prime number multiplicative terms of some nonnegative integer, N.
*
* A prime number is a natural number which is larger than or equal to 2 and which is the multiplicative product of only itself and 1.
*
* A composite number is a natural number which is larger than 1 and which is the multiplicative product of two or more prime numbers.
*/
void print_prime_factorization(int N, std::ostream& output)
{
if (N <= 1)
{
output << "\n\n" << N << " is not factorizable into multiple prime number multiplicative terms.";
return;
}
output << "\n\nPrime factorization of " << N << " is: ";
int divisor = 2; // Start with the smallest prime number.
bool first_factor = true;
while (N > 1)
{
while (N % divisor == 0)
{
if (!first_factor)
{
output << " * "; // Separate factors with a multiplication symbol.
}
output << divisor;
N /= divisor;
first_factor = false;
}
divisor++;
}
output << ".";
}
</pre>
<hr>
<p><strong>SAMPLE_PROGRAM_OUTPUT</strong></p>
<hr>
<p>The text in the preformatted text box below was generated by one use case of the C++ program featured in this <a style="background:#ff9000;color:#000000" href="https://en.wikipedia.org/wiki/Computer_programming" target="_blank" rel="noopener">computer programming</a> tutorial web page.</p>
<p>plain-text_file: <a style="background:#000000;color:#ff9000" href="https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt" target="_blank" rel="noopener">https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt</a></p>
<hr>
<pre>--------------------------------
Start Of Program
--------------------------------
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is 30.
Prime factorization of 30 is: 2 * 3 * 5.
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is 25.
Prime factorization of 25 is: 5 * 5.
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is 0.
0 is not factorizable into multiple prime number multiplicative terms.
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is -1.
Prime factorization of 10 is: 2 * 5.
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is 1.
1 is not factorizable into multiple prime number multiplicative terms.
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is 2.
Prime factorization of 2 is: 2.
Enter a nonnegative integer value to store in the variable named N which is no larger than 10000 to factor into its constituent prime number multiplicative terms:
The value which was entered for N is 144.
Prime factorization of 144 is: 2 * 2 * 2 * 2 * 3 * 3.
--------------------------------
End Of Program
--------------------------------
</pre>
<hr>
<p>This web page was last updated on 21_SEPTEMBER_2024. The content displayed on this web page is licensed as <a style="background:#000000;color:#ff9000" href="https://karlinaobject.wordpress.com/public_domain/" target="_blank" rel="noopener">PUBLIC_DOMAIN</a> intellectual property.</p>
<hr>