PYTHON ASSIGNMENT For this assignment, you shall write a Python script that asks
ID: 3572390 • Letter: P
Question
PYTHON ASSIGNMENT
For this assignment, you shall write a Python script that asks the user for a positive integer between 1 and 1,000. The script shall then print out a list of the prime numbers which are factors of the input value. For the standard assignment, you will update the factors function so that it prints out the prime factorization of a positive integer between 1 and 1,000 that is the parameter. Notice, input validation is performed before running the code that calculates the factors.
Here is a sample run. The user input is shown in bold text.
Enter a integer between 1 and 1,000: 12
The factors of 12: 2 2 3
Notice that the output format has been updated so that all of the factor information appears on one line.
The Algorithm
The algorithm that you will implement will repeatedly check for multiples of the given primes.
Here's the basic algorithm:
Store the input (parameter) value.
Work through the list of primes in order. We'll work with only one prime at a time. This will be called "current prime".
As long as the current prime evenly divides the input value,
Output the current prime.
Set the input value to its quotient when divided by current prime.
When current prime no longer evenly divides the input value, go to the next prime number in the list.
Repeat until value is 1.
If you reach the end of the list of primes and the input value does not equal one, then that remaining input value is prime. Output that value.
Let's look at a couple of concrete examples:
Enter a integer between 1 and 1,000: 264
The factors of 264: 2 2 2 3 11
Here is the algorithm applied to 264:
We start by prompting the user for an input value. In this example, they enter 264.
We can start the output with " The factors of 264:".
Take the value 264 and divide by the current prime number, 2, the first one. It goes in evenly so 2 is factor. We output the factor 2 and update the value to be the quotient, 132.
Take the value 132 and divide by the current prime, 2. It goes in evenly. Output 2 and update value to the the quotient, 66.
Take the value 66 and divide by the current prime, 2. It goes in evenly. Output 2 and update value to 33.
Take the value 33 and divide by the current prime, 2. It doesn't go in evenly. Go on to the next prime.
Take the value 33 and divide by the current prime, 3. It goes in evenly. Output 3 and update value to 11.
Take the value 11 and divide by the current prime, 3. It doesn't go in evenly. Go on to the next prime.
Take the value 11 and divide by the current prime, 5. It doesn't go in evenly. Go on to the next prime.
Take the value 11 and divide by the current prime, 7. It doesn't go in evenly. Go on to the next prime.
Take the value 11 and divide by the current prime, 11. It goes in evenly. Output 11 and update value to 1.
Since value is 1, end the processing.
Enter a integer between 1 and 1,000: 246
The factors of 246: 2 3 41
Here is the algorithm applied to 246:
We start by prompting the user for an input value. In this example, they enter 246.
We can start the output with " The factors of 246:".
Take the value 246 and divide by the current prime, 2. It goes in evenly. Output 2 and update value to 123.
Take the value 123 and divide by the current prime, 2. It doesn't go in evenly. Go on to the next prime.
Take the value 123 and divide by the current prime, 3. It goes in evenly. Output 3 and update value to 41.
Take the value 41 and divide by the current prime, 3. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 5. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 7. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 11. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 13. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 17. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 19. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 23. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 29. It doesn't go in evenly. Go on to the next prime.
Take the value 41 and divide by the current prime, 31. It doesn't go in evenly. Go on to the next prime.
There are no more primes in the list. Output value 41, since it is not equal to 1. End the processing.
Make sure that you end the output line with a newline character. For aesthetic reasons, you could add a second newline character after printing the factors. Now that the function is printing a prime factoring of the input value, make sure you update the expected value for your test cases.
Explanation / Answer
def primes():
l = [2]
for i in range(3,1000):
hat = 0
for k in l:
if(i%k==0):
hat=1
break
if(hat==0):
l.append(i)
return l
def factors():
l = primes()
for i in range(2,1001):
tmp = i
j = 0
an=[]
while i>1:
if(i%l[j]==0):
i=i/l[j]
an.append(l[j])
else:
j=j+1
sn = "The factors of "+str(tmp)+":"
for k in an:
sn = sn+" "+str(k)+" "
print(sn)
factors()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.