PRIME_FACTORIZATION_TWO
The Python 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.
Note that the Python program featured on this web page functions identically (except for how it names the program output file and the fact that it does not print the input prompt nor the input value if that value is “out of range”) to the C++ program featured in the tutorial web page named PRIME_FACTORIZATION. Details about how that program language translation was accomplished (using ChatGPT-4o) are available in the blogging web page at the following Uniform Resource Locator: https://karbytesforlifeblog.wordpress.com/karbytes_23_september_2024/
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
python_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.py
plain_text_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_two_output.txt
PROGRAM_COMPILATION_AND_EXECUTION
STEP_0: Copy and paste the Python source code into a new text editor document and save that document as the following file name:
prime_factorization.py
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: Run the program by entering the following command:
python3 prime_factorization.py
STEP_3: If the program interpretation command does not work, then use the following commands (in top-down order) to install the Python interpreter:
sudo apt update
sudo apt install python3
STEP_4: After running the Python program is booted up, 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_5: Enter a value for N using the keyboard.
STEP_6: 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 Python source code file whose Uniform Resource Locator is displayed in the green hyperlink below.
python_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization.py
# /** # * file: prime_factorization.py # * type: Python # * date: 22_SEPTEMBER_2024 # * author: karbytes # * license: PUBLIC_DOMAIN # */ import os # Define the maximum value for N MAXIMUM_N = 10000 # Function to print the prime factorization of a number N to an output stream (console and file) def print_prime_factorization(N, output): if N 1: while N % divisor == 0: if not first_factor: output.write(" * ") output.write(f"{divisor}") N //= divisor first_factor = False divisor += 1 output.write(".\n") # Program entry point def main(): # Initialize N and input_additional_values N = 0 input_additional_values = 1 # Open the output file for writing with open("prime_factorization_two_output.txt", "w") as file: # Print an opening message to the console and the file print("\n\n--------------------------------") print("Start Of Program") print("--------------------------------") file.write("--------------------------------\n") file.write("Start Of Program\n") file.write("--------------------------------\n") while input_additional_values != 0: # Prompt the user to enter a value for N try: N = int(input(f"\n\nEnter a nonnegative integer value to store in the variable named N which is no larger than {MAXIMUM_N}: ")) if 0 <= N <= MAXIMUM_N: # Print the prime factorization of N to the file and console print_prime_factorization(N, file) print_prime_factorization(N, output=os.sys.stdout) else: print(f"Error: N must be between 0 and {MAXIMUM_N}.") except ValueError: print("Invalid input. Please enter a valid integer.") # Ask if the user wants to input another value input_additional_values = int(input("\n\nWould you like to continue inputting program values? (Enter 1 if YES, Enter 0 if NO): ")) # Print a closing message to the console and the file print("\n\n--------------------------------") print("End Of Program") print("--------------------------------\n\n") file.write("\n\n--------------------------------\n") file.write("End Of Program\n") file.write("--------------------------------\n") if __name__ == "__main__": main()
SAMPLE_PROGRAM_OUTPUT
The text in the preformatted text box below was generated by one use case of the Python program featured in this computer programming tutorial web page.
(Note that the program specifies to name the output file prime_factorization_two.txt instead of prime_factorization.txt because karbytes did not want the C++ program’s output file (which is named prime_factorization.txt) to be overwritten with the Python program output file in the GitHub repository named which houses both output files and which is named KARLINA_OBJECT_extension_pack_20).
plain_text_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_20/main/prime_factorization_two_output.txt
-------------------------------- Start Of Program -------------------------------- Prime factorization of 10 is: 2 * 5. Prime factorization of 12 is: 2 * 2 * 3. Prime factorization of 13 is: 13. 0 is not factorizable into multiple prime number multiplicative terms. Prime factorization of 100 is: 2 * 2 * 5 * 5. Prime factorization of 256 is: 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2. Prime factorization of 201 is: 3 * 67. 1 is not factorizable into multiple prime number multiplicative terms. Prime factorization of 2 is: 2. Prime factorization of 999 is: 3 * 3 * 3 * 37. -------------------------------- End Of Program --------------------------------
This web page was last updated on 22_SEPTEMBER_2024. The content displayed on this web page is licensed as PUBLIC_DOMAIN intellectual property.