Print all prime factors of a given number
Last Updated :
23 Jul, 2025
Improve
Try it on GfG Practice
Given a number n, the task is to find all prime factors of n.
Examples:
Input: n = 24
Output: 2 2 2 3
Explanation: The prime factorization of 24 is 23×3.Input: n = 13195
Output: 5 7 13 29
Explanation: The prime factorization of 13195 is 5×7×13×29.
Approach:
Every composite number has at least one prime factor less than or equal to its square root. This property can be proved using a counter statement -
- Let a and b be two factors of n such that a*b = n.
- If both are greater than sqrt(n), a*b > sqrt(n)*sqrt(n), which contradicts the expression a * b = n
Following are the steps to find all prime factors:
- While n is divisible by 2, print 2 and divide n by 2.
- After step 1, n must be odd. Now start a loop from i = 3 to the square root of n. While i divides n, print i, and divide n by i. After i fails to divide n, increment i by 2 because next prime factor will also be odd since we have already taken care of all the 2 and continue.
- If n greater than 2, then n is the last prime factor.
// C++ program to print all prime factors
#include <bits/stdc++.h>
using namespace std;
// A function to print all prime
// factors of a given number n
void primeFactors(int n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
cout << 2 << " ";
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i*i <= n; i = i + 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
cout << i << " ";
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
cout << n << " ";
}
int main() {
int n = 315;
primeFactors(n);
return 0;
}
// C Program to print all prime factors
# include <stdio.h>
// A function to print all prime factors of a given number n
void primeFactors(int n) {
// Print the number of 2s that divide n
while (n%2 == 0) {
printf("%d ", 2);
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i*i <= n; i = i+2) {
// While i divides n, print i and divide n
while (n%i == 0){
printf("%d ", i);
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
printf ("%d ", n);
}
int main() {
int n = 315;
primeFactors(n);
return 0;
}
// Java Program to print all prime factors
import java.io.*;
import java.lang.Math;
class GfG {
// A function to print all prime factors
// of a given number n
static void primeFactors(int n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i * i <= n; i += 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
System.out.print(n);
}
public static void main(String[] args) {
int n = 315;
primeFactors(n);
}
}
# 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 and 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
n = 315
primeFactors(n)
// C# Program to print all prime factors
using System;
class GfG {
// A function to print all prime
// factors of a given number n
static void primeFactors(int n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
Console.Write(2 + " ");
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i * i <= n; i += 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
Console.Write(i + " ");
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
Console.Write(n);
}
static void Main() {
int n = 315;
primeFactors(n);
}
}
// JavaScript program to print all prime factors
// A function to print all prime
// factors of a given number n
function primeFactors(n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
console.log(2 + " ");
n = Math.floor(n / 2);
}
// n must be odd at this point.
// So we can skip one element
// (Note i = i +2)
for (let i = 3; i <= Math.floor(Math.sqrt(n));
i = i + 2) {
// While i divides n, print i
// and divide n
while (n % i == 0) {
process.stdout.write(i + " ");
n = Math.floor(n / i);
}
}
// This condition is to handle the
// case when n is a prime number
// greater than 2
if (n > 2)
process.stdout.write(n + " ");
}
let n = 315;
primeFactors(n);
Output
3 3 5 7
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Related article:
Prime Factorization
Visit Course