PRIME_FACTORIZATION


The C++ 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.

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.

To view hidden text inside each of the preformatted text boxes below, scroll horizontally.


SOFTWARE_APPLICATION_COMPONENTS


C++_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_21/main/prime_factorization.cpp

plain-text_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt


PROGRAM_COMPILATION_AND_EXECUTION


STEP_0: Copy and paste the C++ source code into a new text editor document and save that document as the following file name:

prime_factorization.cpp

STEP_1: Open a Unix command line terminal application and set the current directory to wherever the C++ is located on the local machine (e.g. Desktop).

cd Desktop

STEP_2: Compile the C++ file into machine-executable instructions (i.e. object file) and then into an executable piece of software named app using the following command:

g++ prime_factorization.cpp -o app

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 GNU Compiler Collection (GCC)):

sudo apt install build-essential
sudo apt-get install g++

STEP_4: After running the g++ command, run the executable file using the following command:

./app

STEP_5: Once the application is running, the following prompt will appear:

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: 

STEP_6: Enter a value for N using the keyboard.

STEP_7: Observe program results on the command line terminal and in the output file.


PROGRAM_SOURCE_CODE


The text in the preformatted text box below appears on this web page (while rendered correctly by the web browser) to be identical to the content of the C++ source code file whose Uniform Resource Locator is displayed in the green hyperlink below. A computer interprets that C++ source code as a series of programmatic instructions (i.e. software) which govern how the hardware of that computer behaves.

(Note that angle brackets which resemble HTML 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 U+003C (which is rendered by the web browser as ‘<‘) and U+003E (which is rendered by the web browser as ‘>’). That is because the WordPress 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)).

C++_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_21/main/prime_factorization.cpp


/**
 * file: prime_factorization.cpp
 * type: C++ (source file)
 * date: 25_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 prime_factorization_output.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 << ".";
}

SAMPLE_PROGRAM_OUTPUT


The text in the preformatted text box below was generated by one use case of the C++ program featured in this computer programming tutorial web page.

plain-text_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_output.txt


--------------------------------
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
--------------------------------

This web page was last updated on 25_SEPTEMBER_2024. The content displayed on this web page is licensed as PUBLIC_DOMAIN intellectual property.