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.
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.
> 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. >
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 factorizationYou should play with this algorithm on paper until you're very confident how it works. Then code it.
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: