In: Computer Science
Python question
Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance, 120 can be factorized into 2 * 2 * 2 * 3 * 5.
Your task is to write a program that will print all prime factors for any integer N entered by the user. Your program also needs to ask the user whether they want to find the prime factors for another input. If the user enters any "y" or "yES" value (e.g., y, Y, yes, yES, yEs, ...), then your program should ask for another number to factor.
Example Execution #1
Enter a positive integer to generate its Prime Factors:
INPUT> 32
The Prime Factors for the integer 32 are:
OUTPUT 2*2*2*2*2
Do you want to get Prime Factors for another input? (y/n)
yeS
-------------------------------------------------------------
Enter a positive integer to generate its Prime Factors:
INPUT> 166
The Prime Factors for the integer 166 are:
OUTPUT 2*83
Do you want to get Prime Factors for another input? (y/n)
no
-------------------------------------------------------------
GoodBye!
HERE IS THE CODE
import math
def primeFactors(n):
while n % 2 == 0: # loop divides the input
number by 2 until it is no longer possible to divide by 2
print (2),
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2): #now
after dividing by 2 completely n is odd now
# while i divides n ,
print i and divide n
while n % i== 0:
print(i),
n = n / i
if n > 2: #if leftover n is prime and greater
than 2
print(n)
n = int(input("INPUT>"))
primeFactors(n)
OUTPUT