Answer to Question #282694 in Python for rahul

Question #282694

SPECIAL FACTORS


Ram loves playing with integers. He has a positive integer N. He wants to find special factors of the number. A factor is a number which exactly divides the given number. A factor is special if it is a prime number. Help ram by identifying all the special factors of the number.


INPUT : The input is a single line containing a positive integer N.


OUTPUT : The output should be a single line containing the space-separated prime factors in ascending order


EXPLANATION : In the example, the given number is 20.

the factors of 20 are 2, 4, 5, 10 Whereas the prime factors are 2, 5.


So, the output should be 2 5


sample input1 : 20


sample output1 : 2 5


sample input 2 : 45


sample output2 : 3 5


1
Expert's answer
2021-12-27T08:39:07-0500
import math
n = int(input())

s = set()
def prime_fator(n):
   while n % 2 == 0:
      s.add(2)
      n = n / 2
    
   for i in range(3,int(math.sqrt(n))+1,2):
     
      while (n % i == 0):
         s.add(i)
         n = n / i
    
   if n > 2:
      s.add(int(n))


prime_fator(n)

print(*s)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS