Question

In: Computer Science

Building a Prime Factorization Factory It is common, and smart, in computing to build upon work...

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

Solutions

Expert Solution

Code:

def isPrime(n):
for i in range(2,int(n/2)):
if(n%i==0):
return False
return True
def getCom():
com = input('Enter a command (factor, isprime, end): ').lower()
while(com!='factor' and com!='isprime' and com!='end'):
print('Command',com ,'not recognized. Try again!\n\n',end='')
com = input('Enter a command (factor, isprime, end): ').lower()
return com


com = 'ww'
print('Welcome to the Prime factory!\n\n',end='')
while(com!='end'):
com = getCom()
if(com=='factor'):
num = int(input('Enter an integer > 1: '))
if(num<2):
print('Illegal input:',num,'; input must be an integer > 1.\n\n',end='')
continue
else:
factors = []
while(num!=1):
for i in range(2,num+1):
if(num%i==0 and isPrime(i)):
factors.append(i)
num=int(num/i)
break
print('The prime factorization of',num,'is',factors,end='\n\n')
if(com=='isprime'):
num = int(input('Enter an integer > 1: '))
if(num<2):
print('Illegal input:',num,'; input must be an integer > 1.\n\n',end='')
continue
else:
if(isPrime(num)):
print('The number',num,' is prime',end='\n\n')
else:
print('The number',num,' is not prime',end='\n\n')

Output:

Hope this helps.


Related Solutions

LMX theory is based upon the personal relationships that we build with the individuals that work...
LMX theory is based upon the personal relationships that we build with the individuals that work for us. As hard as we could try, our personal biases, affinity towards those that have similar interests as ours, and people that we gravitate toward, will tend to remain a part of our primary or at least secondary spheres of engagement. Why? We tend naturally to spend more time with the people at work than we do with our families and work becomes...
ABC Ltd (“ABC”) entered into a contract on 1st January 20x6 to build a factory building...
ABC Ltd (“ABC”) entered into a contract on 1st January 20x6 to build a factory building for its customer on its customer’s land for $20 million. The completion is expected to be in two years’ time. ABC’s policy is to bill its clients based on 60% in the first year and the balance upon completion of the project. ABC has a 31st December financial year end. It has adopted FRS 115 Revenues from Contracts with Customers and adopts the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT