CS303E Project 2

Instructor: Dr. Bill Young
Due Date: Monday, November 17, 2025 at 11:59pm

Building a Prime Factory

It is common, and smart, in computing to build upon work that you or others have done previously, rather than re-inventing the wheel. In this project, you will build on some functions defined in the class slides; some others you'll have to write from scratch. Of course, in this class you should never use work that you find on the internet or done by anyone other than youself (including an AI), except as specifically allowed, as in this case!

Many of the examples we've done in class have involved dealing with prime numbers: decide whether a number is a prime, find the next prime, print the first 100 primes, etc. The concept of a prime number is fundamental in mathematics, particularly in number theory. In fact, the fundamental theorem of arithmetic states that every positive integer (greater than 1) can be represented uniquely (where order doesn't matter) as a product of one or more primes.

Assignment:

Your goal in this project is to write a Prime Factory utility that will allow a user to easily access certain facts about prime numbers. Your program will be menu-driven, which means that the user will be presented with a list of possible commands to select from. There are 7 possible commands, which will be entered as a numeral between 0 and 7 (remember that it comes in as a string). Here are the available commands:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 
You should validate that the command is legal. All commands (except 0 and 6) require an additional piece of information. For those commands, you'll ask the user to specify an integer value N. You will then compute and print the answer using N, or print an error message and the menu. Command 0 exits the program; command 6 asks for information.

It's suggested that you leave the commands as strings, rather than converting them to integers. See the discussion below in the Programming Hints section.

For the commands that require N, you should validate that as well: it must be an integer. There may also be a constraint that it be larger than some K. For commands 1 and 3, N must be positive; for command 2, N must be non-negative; for command 5, N should be at least 2; for command 4, there is no constraint. (I used a single function containing a loop to input and validate N for all of those five commands, passing the constraint value as a parameter.)

If a legal command (and possibly N) is entered, perform the command and display the answer. Within the slides are several functions on primes including isPrime and findNextPrime. Use these; you don't have to re-code them. You'll have to code some others. The hardest one is almost certainly giving the prime factorization. See the section below on that. Below are some output examples.

Expected Output:

Below is some sample output for this program. You should match this exactly.

> python PrimeFactory.py

Welcome to the Prime Factory!
The following commands are available:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 

Please enter a command: 10
Command 10 not recognized. Try again!
The following commands are available:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 

Please enter a command: 0
Thanks for visiting our factory! Goodbye.

> python PrimeFactory.py

Welcome to the Prime Factory!
The following commands are available:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 

Please enter a command: 1
You've asked if N is a prime.
What is N? -10
Illegal value. Try again!
What is N? 1
1 is not prime

Please enter a command: 1
You've asked if N is a prime.
What is N? -10
Illegal value. Try again!
What is N? 1234567
1234567 is not prime

Please enter a command: 2
You've asked to display the first N prime numbers.
What is N? -15
Illegal value. Try again!
What is N? 15
The first 15 primes are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Please enter a command: 3
You've asked for the Nth prime number.
What is N? 0
Illegal value. Try again!
What is N? -5
Illegal value. Try again!
What is N? 15
The 15th prime is: 47

Please enter a command: 4
You've asked for the first prime number after N.
What is N? 100000000 
The first prime after 100000000 is: 100000007

Please enter a command: 4
You've asked for the first prime number after N.
What is N? -1000
The first prime after -1000 is: 2

Please enter a command: 5
You've asked for the prime factorization of N.
What is N? 123456789
The prime factorization of 123456789 is: [3, 3, 3607, 3803]

Please enter a command: 5
You've asked for the prime factorization of N.
What is N? 1
Illegal value. Try again!
What is N? -10
Illegal value. Try again!
What is N? 3803
The prime factorization of 3803 is: [3803]

Please enter a command: 6
The following commands are available:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 

Please enter a command: 0
Thanks for visiting our factory! Goodbye.

> python PrimeFactory.py

Welcome to the Prime Factory!
The following commands are available:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 

Please enter a command: info                     # See Programming Tips below
The following commands are available:
  0: Exit.
  1: Is N a prime?
  2: List the first N prime numbers.
  3: Display the Nth prime number (1-based).
  4: Find first prime after N.
  5: Factor N.
  6: Display this help message. 

Please enter a command: quit                    # See Programming Tips below
Thanks for visiting our factory! Goodbye.

> 
Notice that command 3 displays the Nth prime. The output could be better. E.g., it would display the "1th", "2th", and "3th" primes, instead of the "1st", "2nd", and "3rd". You're welcome to fix that if you like, but you don't have to. (It's a bit harder than you might think. You'd want to produce "23rd", but also "13th", so it depends on more than just the last digit.)

Finding Prime Factorizations:

The only function that may be a bit challenging is finding the prime factorization of a number num. You can use the following algorithm:

set the list of factors to the empty list
set d to 2
while num is not prime and num > 1:
   while d divides num:
       add d to the list of factors
       replace num by num // d
   set d to the next biggest prime
at this point either num is prime or n is 1:
if num is not equal to 1, add it to the list of factors
return the list of factors
You should play with this algorithm on paper until you're very confident how it works. Then code it.

Turning in the Assignment:

The program should be in a file named PrimeFactory.py. Submit the file via Canvas before the deadline shown at the top of this page. Submit it to the assignment project2 under the assignments sections by uploading your python file.

Your file must compile and run before submission. It must also contain a header with the following format:

# Assignment: Project 2
# File: PrimeFactory.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date Created:
# Date Last Modified: 
# Description of Program: 

Programming Tips:

Add robustness: I always find it a good idea to make any system I code as robust and user-friendly as possible. For example, I find it annoying when using a system to have to worry about the case of input commands. Why not make commands case insensitive so that "EnCRypt" works as well as "encrypt"? It's easy to do that; just lowercase the command entered by the user (using comm.lower()) and then compare that to the lowercase version. If they match, accept the command; otherwise, reject it.

Remember: if the command is stored in variable comm, comm.lower() doesn't change comm. You'd want to do something like:

   commLower = comm.lower()
Also, when using a menu driven system like the one you're coding here, I find it annoying when I can't remember either the command to exit, or the command to get the help menu. In my own implementation of the Prime Factory, I allowed 'exit', 'quit', 'leave' as alternatives to exit the program, and allowed 'help' and 'info' as commands that would print the help menu. I typically don't publicize such commands because they're outside the "official" interface. But they make things more user-friendly. You don't have to do this in your program, but you're welcome to if you like. The TAs will only test the official interface.

Choose the correct type: The user inputs a number (0, 1, ..., 6) but it's read as a string. You could convert it to a int, but there's no reason to do that because you never do any arithmetic using these inputs. Just leave them as strings.

There's another reason not to convert for this assignment: If you want to add alternative commands, like "exit", "info", etc. then you don't need to hassel with dealing commands that can be either strings or integers. I defined the following global constant:

LEGAL_COMMANDS = [EXIT, IS_PRIME, LIST_N_PRIMES, SHOW_NTH_PRIME,
                  FIRST_PRIME_AFTER, FACTOR, HELP,
                  'exit', 'quit', 'help', 'leave', 'info']
where EXIT, IS_PRIME, etc are also global constants for the 7 numeral commands. Having this global constant makes it easy to add new ones. A command is syntactically legal if and only if it's in the list.

Integer literals: When Python is reading an integer literal, as in an input statement, you can use underscores to make the number more readable, e.g., 100_000_000. Python simply discards the underscores inside an integer literal. Use them anywhere within the number except at the beginning or end, and you can't have two in a row. So 1_2_3 is legal, but _123, 123_ and 1__23 are not. However, if you're using .isdigit() to validate input to test if a string represents an integer, be careful. Because "1_000".isdigit() is False; to Python "1_000" is just a string, not an integer. However, int("1_000") works fine.