Computer Science, asked by anindyaadhikari13, 4 months ago

Display the prime factors of a number using 1 loop? Write in Java or Python. Show your output.

For example,
Prime factors of 12 are 2 and 3.

Don't spam.
Thank you. ​

Answers

Answered by Oreki
3

Program:

 def prime_factors(number):

     i = 2

     factors = [ ]

     while i * i <= number:

         if number % i:

             i += 1

         else:

             number //= i

             factors.append(i)

     if number > 1:

         factors.append(number)

     return list(set(factors))

   

 number = int(input("Enter a number - "))

 print(f"Prime factors of {number} -", prime_factors(number))

Attachments:

anindyaadhikari13: Thanking you again for your help :)
anindyaadhikari13: Thanks @Oreki.
Oreki: Thanks!
Answered by Mister360
2

# Python program to print prime factors

.import math

# A function to print all prime factors of

# a given number n

def primeFactors(n):

# Print the number of two's that divide n

while n % 2 == 0:

print 2,

n = n / 2

# n must be odd at this point

# so a skip of 2 ( i = i + 2) can be used

for i in range(3,int(math.sqrt(n))+1,2):

# while i divides n , print i ad divide n

while n % i== 0:

print i,

n = n / i

# Condition if n is a prime

# number greater than 2

if n > 2:

print n

# Driver Program to test above function

n = 315

primeFactors(n)

output:-

\sf 3 \:3\: 5\: 7

Similar questions