CS303E Project 2

Instructor: Dr. Bill Young
Due Date: Monday, November 2, 2020 at 11:59pm

Building a Prime Factorization 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 and in the book. Of course, in this class you should never use work that you find on the internet or done by anyone other than youself, 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 (order doesn't matter) as a product of one or more primes. Your task in this project is to write a library of functions to deal with primes including finding the prime factorization of arbitrary positive integers.

Assignment:

When the program begins it should print the message ``Welcome to the Prime factory!'' Then, accept from the user a command, which can be one of the following: factor, isprime, or end. Case does not matter for this command. Example: end, EnD, and END should all behave identically. (You don't have to preserve the case of the input string in error messages.) If the user enters anything else, print an error message. See the examples below.

If the user enters factor, prompt for an integer greater than 1. You can assume that the input is an integer, but don't assume it is greater than 1. If it's not, print an error message and return to the top level loop (i.e., back to prompting for a command). If an integer greater than 1 is entered, compute and print its prime factorization as a list. See the examples below.

If the user enters isprime, prompt for an integer. You can assume that the input is an integer. If the supplied value is less than 2, print an error message and return to the top level loop. Otherwise, check whether the input is prime and print an appropriate message. See the examples below.

Finally, if the user enters end, print the message ``Thanks for using our service!'' and exit.

Put a blank line between each round of interaction to make the output more readible.

Expected Output:

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

> python Project2.py
Welcome to the Prime factory!

Enter a command (factor, isprime, end): fector
Command fector not recognized. Try again!

Enter a command (factor, isprime, end): fAcToR
Enter an integer > 1: 1000
The prime factorization of 1000 is: [2, 2, 2, 5, 5, 5]

Enter a command (factor, isprime, end): facTOR
Enter an integer > 1: 213
The prime factorization of 213 is: [3, 71]

Enter a command (factor, isprime, end): factor
Enter an integer > 1: -23
Illegal input: -23; input must be an integer > 1.

Enter a command (factor, isprime, end): factor 100
Command factor 100 not recognized. Try again!

Enter a command (factor, isprime, end): factor
Enter an integer > 1: 1009
The prime factorization of 1009 is: [1009]

Enter a command (factor, isprime, end): isPRIME 
Enter an integer > 1: 1009
The number 1009 is prime

Enter a command (factor, isprime, end): ISprime 213
Command isprime 213 not recognized. Try again!

Enter a command (factor, isprime, end): ISprime 
Enter an integer > 1: 213
The number 213 is not prime

Enter a command (factor, isprime, end): IsPrime
Enter an integer > 1: -23
Illegal input: -23; input must be an integer > 1.

Enter a command (factor, isprime, end): isPrime?
Command isprime? not recognized. Try again!

Enter a command (factor, isprime, end): EnD
Thanks for using our service! Goodbye.
>

Finding the Prime Factorization:

Within the slides are several functions on primes including isPrime and findNextPrime. Use these; you don't have to re-code them. You can find the prime factorization of a number num with the following algorithm:

if num is prime:
   return a list containing only num

otherwise num is composite:
   set the list of factors to the empty list
   set d to 2
   as long as num is greater than 1 do the following:
      check if d divides num
         if it does, add d to the list of factors
            and divide num by d
         keep checking d until it doesn't divide num
      set d to the next biggest prime
   at this point num is 1 and the list of factors is the prime factorization
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 Project2.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:

# File: Project2.py
# Student: 
# UT EID:
# Course Name: CS303E
# 
# Date Created:
# Date Last Modified: 
# Description of Program: