Open In App

Python Program to Print all Prime Factors of a given Number

Last Updated : 29 Oct, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

Given a number n, the task is to print all of its prime factors. Prime factors are the prime numbers that divide a given number exactly without leaving a remainder. For example:

Input: 12
Output: 2 2 3

Let's explore different methods to find all prime factors of a number in Python.

Using Sieve of Eratosthenes

In this method, we precompute smallest prime factor for every number up to n using a sieve-like approach. Then, we use these precomputed values to efficiently print all prime factors of the given number.

Python
n = 315
spf = [0 for _ in range(n + 1)]  # Smallest Prime Factor array
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, 2):
    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])
    n //= spf[n]

Output
3
3
5
7

Explanation:

  • Creates an array spf (smallest prime factor) for all numbers up to n and assigns each even number’s smallest factor as 2.
  • For odd numbers, updates multiples of primes starting from their square.
  • Uses the precomputed spf array to divide n continuously until 1.
  • Each printed value is a prime factor of the number.

Using Math Module

Here, we use math module to divide the number continuously by 2 and then by all odd divisors up to its square root. Each divisor that divides the number completely is printed as a prime factor.

Python
import math
n = 315

while n % 2 == 0:
    print(2)
    n //= 2

for i in range(3, int(math.sqrt(n)) + 1, 2):
    while n % i == 0:
        print(i)
        n //= i

if n > 2:
    print(n)

Output
3
3
5
7

Explanation:

  • Removes all factors of 2 first and then checks divisibility by all odd numbers up to √n.
  • Prints each divisor whenever it divides the number completely.
  • If n remains greater than 2 at the end, it is printed as the last prime factor.

Using Iterative Division

In this method, number is divided repeatedly by the smallest divisor starting from 2. Each divisor that divides the number is printed immediately, and the process continues until the number becomes 1.

Python
n = 315
i = 2

while i * i <= n:
    while n % i == 0:
        print(i)
        n //= i
    i += 1

if n > 1:
    print(n)

Output
3
3
5
7

Explanation:

  • Starts checking divisibility from 2 and goes upward and removes each divisor completely when it divides n.
  • Continues until all smaller factors are removed.
  • The remaining number greater than 1 (if any) is printed as the final prime factor.

Using Anonymous Function

Here, we use a lambda expression to generate prime factors of the number dynamically. The code repeatedly finds and removes each factor until the number is reduced to 1.

Python
pf = 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
f = []

while n > 1:
    for factor in pf(n):
        f.append(factor)
        n //= factor

print(*f)

Output
3 5 7 3

Explanation:

  • lambda function finds all prime divisors of n and each found factor is stored and used to reduce n.
  • Then loops until n becomes 1, printing all prime factors and uses built-in list comprehension and dynamic filtering for simplicity.

Optimizations of All Divisors and Prime in Python
Visit Course explore course icon
Article Tags :

Explore