The Wayback Machine - https://web.archive.org/web/20241127012525/https://www.geeksforgeeks.org/cpp-program-to-find-all-factors-of-a-natural-number/
Open In App

C++ Program To Find All Factors of A Natural Number

Last Updated : 17 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a natural number n, print all distinct divisors of it.

 all divisors of a natural number

Examples:

 Input : n = 10       
 Output: 1 2 5 10

 Input:  n = 100
 Output: 1 2 4 5 10 20 25 50 100

 Input:  n = 125
 Output: 1 5 25 125

Note that this problem is different from finding all prime factors.

A Naive Solution would be to iterate all the numbers from 1 to n, checking if that number divides n and printing it. Below is a program for the same:

C++




// C++ implementation of Naive 
// method to print all divisors
#include <iostream>
using namespace std;
  
// Function to print the divisors
void printDivisors(int n)
{
    for (int i = 1; i <= n; i++)
        if (n % i == 0)
            cout <<" " << i;
}
  
// Driver code
int main()
{
    cout <<"The divisors of 100 are: ";
    printDivisors(100);
    return 0;
}
  
// This code is contributed by shivanisinghss2110


Output:

The divisors of 100 are: 
1 2 4 5 10 20 25 50 100

Time Complexity : O(n) 
Auxiliary Space : O(1)

Can we improve the above solution? 
If we look carefully, all the divisors are present in pairs. For example if n = 100, then the various pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10)
Using this fact we could speed up our program significantly. 
We, however, have to be careful if there are two equal divisors as in the case of (10, 10). In such case, we’d print only one of them. 

Below is an implementation for the same:

C++




// A Better (than Naive) Solution 
// to find all divisors
#include <iostream>
#include <math.h>
using namespace std;
  
// Function to print the divisors
void printDivisors(int n)
{
    // 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)
                cout <<" "<< i;
  
            // Otherwise print both
            else 
                cout << " "<< i << " " << n / i;
        }
    }
}
  
// Driver code
int main()
{
    cout <<"The divisors of 100 are: ";
    printDivisors(100);
    return 0;
}
  
// This code is contributed by shivanisinghss2110


Output:

The divisors of 100 are: 
1 100 2 50 4 25 5 20 10

Time Complexity: O(sqrt(n)) 
Auxiliary Space : O(1)

However there is still a minor problem in the solution, can you guess? 
Yes! the output is not in a sorted fashion which we had got using the brute-force technique. Please refer below for an O(sqrt(n)) time solution that prints divisors in sorted order.
Find all divisors of a natural number | Set 2



Similar Reads

Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
Given a perfect square natural number N. The task is to find all the factors of N. Examples Input: N = 100 Output: 1 2 4 5 10 20 25 50 100 Input: N = 900 Output: 1 2 4 3 6 12 9 18 36 5 10 20 15 30 60 45 90 180 25 50 100 75 150 300 225 450 900 Approach: Find the square root of N in temp.Find all the prime factors of temp in O(sqrt(temp)) using the a
15+ min read
Find all factors of a Natural Number in sorted order
Given a natural number n, print all distinct divisors of it. Examples: Input : n = 10 Output: 1 2 5 10 Input: n = 100 Output: 1 2 4 5 10 20 25 50 100 Input: n = 125 Output: 1 5 25 125 We strongly recommend referring to the below article as a prerequisite. Find all divisors of a natural number | Set 1In the above post, we found a way to find all the
10 min read
Find all factors of a Natural Number
Given a natural number n, print all distinct divisors of it. Examples: Input: n = 10 Output: 1 2 5 10Explanation: 1, 2, 5 and 10 are the factors of 10. Input: n = 100Output: 1 2 4 5 10 20 25 50 100Explanation: 1, 2, 4, 5, 10, 20, 25, 50 and 100 are factors of 100. Input: n = 125Output: 1 5 25 125 Note that this problem is different from finding all
7 min read
Find number of factors of N when location of its two factors whose product is N is given
Given a and b which represent the location of two factors of N whose product is equal to the number N when the factors are arranged in ascending order. The task is to find the total number of factors of N.Examples: Input : a = 2, b = 3 Output : 4 N = 6 factors are {1, 2, 3, 6} No of factors are 4 Input : a = 13, b = 36 Output : 48 Approach: The sol
4 min read
Maximum number of prime factors a number can have with exactly x factors
Given an integer X, denoting the number of factors of a positive integer N can have. The task is to find the maximum number of distinct prime factors the number N can have. Examples: Input: X = 9 Output: 2 Explanation: Some of the possible numbers having 9 factors are: 256: 1, 2, 4, 8, 16, 32, 64, 128, 256 Number of prime factors = 1 36: 1, 2, 3, 4
6 min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Check if a number exists having exactly N factors and K prime factors
Given two numbers N and K, the task is to find whether an integer X exists such that it has exactly N factors and K out of them are prime.Examples: Input: N = 4, K = 2 Output: Yes Explanation: One possible number for X is 6. The number 6 has a total 4 factors: 1, 2, 3 & 6. It also has exactly 2 prime factors: 2 & 3.Input: N = 3, K = 1 Outpu
15+ min read
Program to find all Factors of a Number using recursion
Given a number N, the task is to print all the factors of N using recursion.Examples: Input: N = 16 Output: 1 2 4 8 16 Explanation: 1, 2, 4, 8, 16 are the factors of 16. A factor is a number which divides the number completely. Input: N = 8 Output: 1 2 4 8 Approach: The idea is to create a function that takes 2 arguments. The function is recursivel
3 min read
Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].Note: The product may be very large, take modulo to 109 + 7. Examples: Input: arr[] = {4, 7, 5, 9, 3}, M =
13 min read
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 the output should be "2 2 3". And if the input number is 315, then the output should be "3 3 5 7". Recommended PracticePrime factorization and geek numberTry It!First Approach:Following are the steps to find all prime factors.
9 min read
Find all digits of given number N that are factors of N
Given a number N, the task is to print the digits of N which divide N. Example: Input: N = 234Output: 2 3 Input: N = 555Output: 5 Approach: The idea is to iterate over all the digits of N and check whether that digit divides N or not. Follow the steps below to solve the problem: Initialize the variables tem as N.Traverse over the while loop till te
4 min read
Find all the factors of given Number and their bitwise XOR
Given an integer N, the task is to find all the factors (excluding itself) of the number N and their XOR. Examples: Input: N = 8 Output:Divisors are: 1 2 4 Xor: 7Explanation: 1, 2 and 4 are all factors of 8 and 1^2^4 = 7. Input: N = 25Output:Divisors are: 1 5 Xor: 4 Approach: Follow the below idea to solve the problem: Store all the factors of the
6 min read
Queries on sum of odd number digit sums of all the factors of a number
Given Q queries. Each query contain a positive integer n. The task is to output the sum of sum of odd number digit contained in all the divisors of n.Examples : Input : Q = 2, n1 = 10, n2 = 36 Output : 7 18 For Query1, Divisors of 10 are 1, 2, 5, 10. Sum of odd digits in 1 is 1, in 2 is 0, in 5 is 5, in 10 is 1. So, sum became 7.For Query 2, Diviso
9 min read
C++ Program to find sum of even factors of a number
Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, â€Ķ pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (
3 min read
Find sum of divisors of all the divisors of a natural number
Given a natural number n, the task is to find sum of divisors of all the divisors of n. Examples: Input : n = 54 Output : 232 Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54. Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 are 1, 3, 4, 12, 13, 39, 40, 120 respectively. Sum of divisors of all the divisors of 54 = 1 + 3 + 4 + 12 + 13 + 39 + 40 + 120 = 232. I
7 min read
Number of factors of very large number N modulo M where M is any prime number
Given a large number N, the task is to find the total number of factors of the number N modulo M where M is any prime number. Examples: Input: N = 9699690, M = 17 Output: 1 Explanation: Total Number of factors of 9699690 is 256 and (256 % 17) = 1Input: N = 193748576239475639, M = 9 Output: 8 Explanation: Total Number of factors of 9699690 is 256 an
8 min read
Program to find the Nth natural number with exactly two bits set
Given an integer N, the task is to find the Nth natural number with exactly two bits set. Examples: Input: N = 4 Output: 9 Explanation: Numbers with exactly two bits set: 3, 5, 6, 9, 10, 12, ... 4th number in this is 9Input: N = 15 Output: 48 Naive Approach: Run a loop through all natural numbers, and for each number, check if it has two bits set o
4 min read
Program to find the Nth natural number with exactly two bits set | Set 2
Given an integer N, the task is to find the Nth natural number with exactly two set bits. Examples: Input: N = 4Output: 9Explanation: Numbers with exactly two set bits are 3, 5, 6, 9, 10, 12, .... The 4th term in this series is 9. Input: N = 15Output: 48Explanation: Numbers with exactly two set bits are 3, 5, 6, 9, 10, 12, 17, 18, 20, 24, 33, 34, 3
7 min read
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers
Given a number N (N > 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers â‰Ī N that are even and can be expressed as the product of two distinct prime numbers. Example: Input: N = 6Output: 2→1 3→1Explanation: 6 is the only number â‰Ī N, which is even and a product of two distinct prime numbers (2 an
7 min read
Check if all Prime factors of number N are unique or not
Given a number N. The task is to check whether the given number N has unique prime factors or not. If yes then print YES else print NO.Examples: Input: N = 30 Output: YES Explanation: N = 30 = 2*3*5 As all the prime factors of 30 are unique.Input: N = 100 Output: NO Explanation: N = 100 = 2*2*5*5 As all the prime factors of 100 are not unique becau
8 min read
Sum of all the factors of a number
Given a number n, the task is to find the sum of all the factors.Examples : Input : n = 30 Output : 72 Dividers sum 1 + 2 + 3 + 5 + 6 + 10 + 15 + 30 = 72 Input : n = 15 Output : 24 Dividers sum 1 + 3 + 5 + 15 = 24 Recommended PracticeFactors SumTry It! A simple solution is to traverse through all divisors and add them. C/C++ Code // Simple C++ prog
12 min read
Program to print factors of a number in pairs
Given a number n, the task of the programmer is to print the factors of the number in such a way that they occur in pairs. A pair signifies that the product of the pair should result in the number itself. Examples: Input : 24Output : 1*24 2*12 3*8 4*6Input : 50Output : 1*50 2*25 5*10Recommended: Please try your approach on {IDE} first, before movin
7 min read
Efficient program to print the number of factors of n numbers
Given an array of integers. We are required to write a program to print the number of factors of every element of the given array.Examples: Input: 10 12 14 Output: 4 6 4 Explanation: There are 4 factors of 10 (1, 2, 5, 10) and 6 of 12 and 4 of 14. Input: 100 1000 10000 Output: 9 16 25 Explanation: There are 9 factors of 100 and 16 of 1000 and 25 of
15 min read
Find all divisors of first N natural numbers
Given an integer N, the task is to find all the divisors of numbers from 1 to N.Note: 1 ? N ? 100000 Examples: Input: N = 2 Output: 1 -->1 2 -->1, 2 Input: N = 5 Output: 1 -->1 2 -->1, 2 3 -->1, 3 4 -->1, 2, 4 5 -->1, 5 Naive Approach: Iterate over first N natural numbers using a loop variable (say i)Iterate over the natural nu
11 min read
Find sum of odd factors of a number
Given a number n, the task is to find the odd factor sum.Examples : Input : n = 30Output : 24Odd dividers sum 1 + 3 + 5 + 15 = 24 Input : 18Output : 13Odd dividers sum 1 + 3 + 9 = 13 Approach :- Naive approach in o(n) time complexity The first approach involves iterating over all factors of the given number and checking if each factor is odd. If a
10 min read
Find minimum sum of factors of number
Given a number N, the task is to find minimum sum of its factors. Examples: Input: 12Output: 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore minimum sum is 7 Input: 105Output: 15 Recommended Pra
4 min read
Queries to find whether a number has exactly four distinct factors or not
Given a positive integers 'q' and 'n'. For each query 'q' find whether a number 'n' have exactly four distinct divisors or not. If the number have exactly four divisors then print 'Yes' else 'No'.1 <= q, n <= 106 Input: 2 10 12 Output: Yes No Explanation: For 1st query, n = 10 has exactly four divisor i.e., 1, 2, 5, 10. For 2nd query, n = 12
11 min read
Find sum of even factors of a number
Given a number n, the task is to find the even factor sum of a number.Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Recommended PracticeFind sum of even factors of a numberTry It! Prerequisite : Sum of factorsAs discussed in above mentioned previous post, sum of fact
8 min read
Greatest divisor which divides all natural number in range [L, R]
Given two integers L and R, the task is to find the greatest divisor that divides all the natural numbers in the range [L, R].Examples: Input: L = 3, R = 12 Output: 1Input: L = 24, R = 24 Output: 24 Approach: For a range of consecutive integer elements, there are two cases: If L = R then the answer will L.If L < R then all consecutive natural nu
3 min read
Sum of all proper divisors of a natural number
Given a natural number, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number. For example, number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.Examples : Input : num = 10 Output: 8 // proper divisors 1 + 2 + 5 = 8 Input
6 min read
Practice Tags :
three90RightbarBannerImg