# Python Program for Efficient program to print all prime factors of a given number

Given a number n, write an efficient function to print all prime factors of n.

For example, if the input number is 12, then output should be “2 2 3”. And if the input number is 315, then output should be “3 3 5 7”. Following are the steps to find all prime factors.

1) While n is divisible by 2, print 2 and divide n by 2.

2) After step 1, n must be odd. Now start a loop from i = 3 to square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.

3) If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.

## Python

 `# 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)`   `# This code is contributed by Harshit Agrawal ` `#Code Improved by Sarthak Shrivastava`

Output:

`3 3 5 7`

Time complexity: O(sqrt(n))
Auxiliary space: O(1)

How does this work?

The steps 1 and 2 take care of composite numbers and step 3 takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers. This is clear that step 1 takes care of even numbers. And after step 1, all remaining prime factor must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2.

Now the main part is, the loop runs till square root of n not till n.

To prove that this optimization works, let us consider the following property of composite numbers.

Every composite number has at least one prime factor less than or equal to square root of itself.

This property can be proved using counter statement. Let a and b be two factors of n such that a*b = n. If both are greater than âˆšn, then a.b > âˆšn, * âˆšn, which contradicts the expression “a * b = n”.

In step 2 of the above algorithm, we run a loop and do following in loop

a) Find the least prime factor i (must be less than âˆšn,)

b) Remove all occurrences i from n by repeatedly dividing n by i.

c) Repeat steps a and b for divided n and i = i + 2. The steps a and b are repeated till n becomes either 1 or a prime number. Please refer complete article on Efficient program to print all prime factors of a given number for more details!

#### Approach#2: Using Sieve of Eratosthenes

This approach prints all the prime factors of a given number ‘n’. It first initializes an array ‘spf’ with indices from 0 to n+1 with values set to 0. It then assigns the value of ‘i’ to the corresponding index in the array for all ‘i’ from 2 to n. For even numbers, the value of 2 is assigned to the corresponding index in the array. Then, for all odd numbers ‘i’ from 3 to the square root of ‘n’, the array index ‘spf[i]’ is checked, and if it equals to ‘i’, then all multiples of ‘i’ in the range from ‘i*i’ to ‘n’ are checked and their corresponding array values are set to ‘i’. Finally, the prime factors of the given number are printed using the array ‘spf’.

#### Algorithm

1. Create a boolean array “prime[0..n]” and initialize all entries it as true.
2. Mark all the multiples of 2, 3, 5, …, sqrt(n) as not prime. Here, instead of marking, we store the smallest prime factor for every composite number.
3. Traverse the array from smallest prime factor of i to sqrt(n) while i divides n. The smallest prime factor of n will be a prime factor.
4. If n is a prime number and greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.

## Python3

 `def` `primeFactors(n):` `    ``spf ``=` `[``0` `for` `i ``in` `range``(n``+``1``)]` `    ``spf[``1``] ``=` `1` `    ``for` `i ``in` `range``(``2``, n``+``1``):` `        ``spf[i] ``=` `i` `    ``for` `i ``in` `range``(``4``, n``+``1``, ``2``):` `        ``spf[i] ``=` `2` ` `  `    ``for` `i ``in` `range``(``3``, ``int``(n``*``*``0.5``)``+``1``):` `        ``if` `spf[i] ``=``=` `i:` `            ``for` `j ``in` `range``(i``*``i, n``+``1``, i):` `                ``if` `spf[j] ``=``=` `j:` `                    ``spf[j] ``=` `i` `                     `  `    ``while` `n !``=` `1``:` `        ``print``(spf[n], end``=``" "``)` `        ``n ``=` `n ``/``/` `spf[n]` ` `  `# example usage` `n ``=` `315` `primeFactors(n)`

Output

`3 3 5 7 `

Time Complexity: O(n*log(log(n)))
Space Complexity: O(n)

Approach#3: Using anonymous function

The approach uses anonymous function to generate all prime factors of a given number. It then uses a while loop and for loop to repeatedly call the function and append the factors to a list until the given number n is reduced to 1. Finally, it prints the prime factors of n separated by a space.

#### Algorithm

1. Define an anonymous function prime_factors that takes a positive integer n as input and generates all prime factors of n.
2. Initialize an empty list factors.
3. Use a while loop to repeatedly call the prime_factors function and append the factors to factors until n is reduced to 1.
4. Inside the while loop, use a for loop to iterate over the prime factors of n generated by the prime_factors function.
5. Append each factor to factors and update n by dividing it by the factor.
6. Print the prime factors of n separated by a space using the print function with the * operator to unpack the list of factors as arguments.

## Python3

 `# Using anonymous function` `prime_factors ``=` `lambda` `n: [i ``for` `i ``in` `range``(``2``, n``+``1``) ``if` `n``%``i ``=``=` `0` `and` `all``(i ``%` `j !``=` `0` `for` `j ``in` `range``(``2``, ``int``(i``*``*``0.5``)``+``1``))]` `n ``=` `315` `factors ``=` `[]` `while` `n > ``1``:` `    ``for` `factor ``in` `prime_factors(n):` `        ``factors.append(factor)` `        ``n ``/``/``=` `factor` `print``(``*``factors)`

Output

`3 5 7 3`

Time complexity: O(n^1.5)
Auxiliary Space: O(n) or O(log n)

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next