Find all factors of a Positive Number
Last Updated :
14 Jul, 2025
Given a positive integer n, find all the distinct divisors of n.
Examples:
Input: n = 10
Output: [1, 2, 5, 10]
Explanation: 1, 2, 5 and 10 are the divisors of 10.
Input: n = 100
Output: [1, 2, 4, 5, 10, 20, 25, 50, 100]
Explanation: 1, 2, 4, 5, 10, 20, 25, 50 and 100 are divisors of 100.
[Naive Approach] Iterating till n - O(n) Time and O(1) Space
The idea is to iterate over all the numbers from 1 to n and for each number check if the number divides n. If the number divides n, print it.
C++
#include <iostream>
#include <vector>
using namespace std;
vector<int> printDivisors(int n) {
vector<int>divisors;
for (int i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i == 0) {
divisors.push_back(i);
}
}
return divisors;
}
int main() {
vector<int>divisors = printDivisors(10);
for(auto &div: divisors) {
cout << div << " " ;
}
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
int* printDivisors(int n, int* size) {
int* divisors = (int*)malloc(n * sizeof(int));
*size = 0;
for (int i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i == 0) {
divisors[(*size)++] = i;
}
}
return divisors;
}
int main() {
int size;
int* divisors = printDivisors(10, &size);
for (int i = 0; i < size; i++) {
printf("%d ", divisors[i]);
}
free(divisors);
return 0;
}
Java
import java.util.*;
public class Main {
public static ArrayList<Integer> printDivisors(int n) {
ArrayList<Integer> divisors = new ArrayList<>();
// Iterate from 1 to n and check divisibility
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
// If 'i' divides 'n' evenly, it's a divisor
divisors.add(i);
}
}
// Return the list of divisors
return divisors;
}
public static void main(String[] args) {
int number = 10;
ArrayList<Integer> divisors = printDivisors(number);
for (int div : divisors) {
System.out.print(div + " ");
}
}
}
Python
def printDivisors(n):
# Create a list to store divisors
divisors = []
# Iterate from 1 to n and check divisibility
for i in range(1, n + 1):
if n % i == 0:
# If 'i' divides 'n' evenly, it's a divisor
divisors.append(i)
return divisors
if __name__ == "__main__":
number = 10
divisors = printDivisors(number)
for div in divisors:
print(div, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int> printDivisors(int n) {
List<int> divisors = new List<int>();
for (int i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i == 0) {
divisors.Add(i);
}
}
return divisors;
}
static void Main() {
List<int> divisors = printDivisors(10);
foreach (int div in divisors) {
Console.Write(div + " ");
}
}
}
JavaScript
function printDivisors(n) {
let divisors = [];
for (let i = 1; i <= n; i++) {
// i is a divisor of n
if (n % i === 0) {
divisors.push(i);
}
}
return divisors;
}
// Driver Code
let divisors = printDivisors(10);
for (let div of divisors) {
process.stdout.write(div + " ");
}
[Expected Approach] Finding all factors in pairs - O(sqrt(n)) Time and O(1) Space
If we look carefully, all the divisors of a number appear in pairs.
For example, if n = 100, then the divisor pairs are:
(1, 100), (2, 50), (4, 25), (5, 20), (10, 10).
We need to be careful in cases like (10, 10)—i.e., when both divisors in a pair are equal (which happens when n is a perfect square). In such cases, we should include that divisor only once.
Using this fact, we can optimize our program significantly.
Instead of iterating from 1 to n, we only need to iterate from 1 to √n.
Why? Because for any factor a of n, the corresponding factor b = n / a forms a pair (a, b).
At least one of the two values in any such pair must lie within the range [1, √n].
So, we can:
- Iterate from 1 to √n to find all divisors less than or equal to √n.
- For each such divisor d, also add n / d as the paired divisor.
- Take care not to duplicate the square root divisor if n is a perfect square.
C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<int> printDivisors(int n) {
vector<int>divisors;
// Note that this loop runs till square root
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, print only one
if (n / i == i) {
divisors.push_back(i) ;
}
// Otherwise print both
else {
divisors.push_back(i) ;
divisors.push_back(n/i) ;
}
}
}
return divisors;
}
int main() {
vector<int>divisors = printDivisors(10);
for(auto &divs: divisors) {
cout << divs << " " ;
}
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int* printDivisors(int n, int *size)
{
*size = 0;
// Rough upper bound for number of divisors
int capacity = 2 * (int)sqrt(n);
int* divisors = (int*)malloc(capacity * sizeof(int));
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) {
// is both i and n/i is same
divisors[(*size)++] = i;
} else {
// if i and n/i is different
divisors[(*size)++] = i;
divisors[(*size)++] = n / i;
}
}
}
return divisors;
}
int main() {
int size;
int* divisors = printDivisors(10, &size);
for (int i = 0; i < size; i++) {
printf("%d ", divisors[i]);
}
free(divisors);
return 0;
}
Java
import java.util.ArrayList;
public class Main {
static ArrayList<Integer> printDivisors(int n) {
ArrayList<Integer> divisors = new ArrayList<>();
// Note that this loop runs till square root
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, add only once
if (n / i == i) {
divisors.add(i);
}
// Otherwise add both
else {
divisors.add(i);
divisors.add(n / i);
}
}
}
return divisors;
}
public static void main(String[] args) {
ArrayList<Integer> divisors = printDivisors(10);
for (int divs : divisors) {
System.out.print(divs + " ");
}
}
}
Python
import math
def printDivisors(n):
divisors = []
# Loop runs up to square root of n
for i in range(1, int(math.sqrt(n)) + 1):
if n % i == 0:
# If both divisors are same (perfect square), add only once
if n // i == i:
divisors.append(i)
else:
# Add both divisors
divisors.append(i)
divisors.append(n // i)
return divisors
if __name__ == "__main__":
number = 10
divisors = printDivisors(number)
for div in divisors:
print(div, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int> printDivisors(int n) {
List<int> divisors = new List<int>();
// Note that this loop runs till square root
for (int i = 1; i <= Math.Sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, print only one
if (n / i == i) {
divisors.Add(i);
}
// Otherwise print both
else {
divisors.Add(i);
divisors.Add(n / i);
}
}
}
return divisors;
}
static void Main() {
List<int> divisors = printDivisors(10);
foreach (int divs in divisors) {
Console.Write(divs + " ");
}
}
}
JavaScript
// Function to print the divisors
function printDivisors(n) {
let divisors = [];
// Note that this loop runs till square root
for (let i = 1; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
// If divisors are equal, print only one
if (n / i === i) {
divisors.push(i);
}
// Otherwise print both
else {
divisors.push(i);
divisors.push(n / i);
}
}
}
return divisors;
}
// Driver Code
let n = 10;
let divisors = printDivisors(n);
for (let divs of divisors) {
process.stdout.write(divs + " ");
}
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem