How to Find Divisors of a Number?

A number that equally divides a larger integer is referred to as a divisor or factor. By simply listing all the possible methods to multiply two numbers together to obtain a small integer, it is simple to discover how many divisors that integer has. When dealing with larger integers, figuring out how many divisors there are becomes more difficult. So, we are going to build a program to find all the divisors of a particular number or an integer. We are also going to calculate the Time complexity and the Space complexity of the program also.

Approach for the Program

One simple approach would be to cycle through all the integers from 1 to n, testing each one to see if it divides n before printing it. Below is a program for the same.

Example 1:

// 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 program to test above function */
int main()
{
	cout <<"The divisors of 100 are: \n";
	printDivisors(100);
	return 0;
}

Output:

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

Time Complexity for the following program is - O(n)

The Space Complexity for the following program is - O(1)

Approach 2

All the divisors will be represented in pairs if we pay close attention. The different pairings of divisors, for instance, are (1,100), (2,50), (4,25), and (5,20) if n = 100. (10,10). We could considerably speed up our program by using this fact. However, if there are two equal divisors, as there are in the case of (10, 10). We would just print one of them in this scenario.

Example 2:

// 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;


			else // Otherwise print both
				cout << " "<< i << " " << n/i;
		}
	}
}


/* Driver program to test above function */
int main()
{
	cout <<"The divisors of 100 are: \n";
	printDivisors(100);
	return 0;
}

Output:

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

Time Complexity for the following program is -  O(sqrt(n))

Space Complexity for the following program is - O(1)

Approach 3

In the method below, we first print the only numbers that have a remainder of 0, and then, after we leave the loop, we only display the quotient of numbers that has a remainder of 0. If we look at the results we obtained, we can see that the divisors are displayed in a zigzag pattern (small, large pairs). So long as we store half of them, we can print them in sorted order. The if condition is used between the two loops when the corner factors in the loops condition differ by 1 (for example, factors of 30 (5,6)here, 5 would be printed twice); this step is necessary to fix the problem. let's clarify this using an example.

Example 3:

#include <iostream>
#include <math.h>
using namespace std;


// Function to print the divisors
void printDivisors(int n)
{
	int i;
	for (i = 1; i * i < n; i++) {
		if (n % i == 0)
			cout<<i<<" ";
	}
	if (i - (n / i) == 1) {
		i--;
	}
	for (; i >= 1; i--) {
		if (n % i == 0)
			cout<<n / i<<" ";
	}
}


// Driver code
int main()
{
	cout << "The divisors of 100 are: \n";


	printDivisors(100);


	return 0;
}

Output:

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

Time Complexity for the following program is - O(sqrt(n))

Space Complexity for the following program is - O(1)


Related Topics

How to unarchive in Gmail?

How to unarchive in Gmail Earlier, we have learned how to archive an email on a computer or a phone device via the Google app. But archiving is not sufficient; you...

3 minutes read.

How to accelerate the speed of loading images on any website?

No website can be successful without attractive and relevant images or pictures. In the current time, the users want speedy information about their searches on the internet browser. The image...

3 minutes read.

How to select all in Gmail

How to select all in Gmail Undoubtedly Gmail is one of the leading email service providers across the world. Teenagers, adults, old ages all rely upon Gmail because of its free...

3 minutes read.

How to block emails on Gmail?

How to block emails on Gmail? Email or electronic mail is shared on the internet, including content, website URL, signature and different forms of attachments (unlike images, mp4, mp3, Pdf, excel,...

7 minutes read.

How to find your Computer’s specs?

How to find your computer’s specs The computer specs include the details for every single component in your computer, including your CPU, hard drive space, and even the little things like...

7 minutes read.

How to snooze an email in Gmail

How to snooze an email in Gmail With the popularity of Gmail, today every deal is likely to be discussed in emails. And with so many things rushing in the Inbox,...

3 minutes read.

How to Find Element in Rotated Sorted Array?

In this article, we are going to find the element in a rotated sorted array using the programs in different computer languages like Java and C++. We are going to...

3 minutes read.

How to defrag a computer?

How to defrag a computer Topics Covered What is Defragmentation?What is the need of defragmentation?Advantages of DefragmentationWhen to defrag your computerDefragmentation for your SSDDefrag your computer running on windows 10Defrag your computer...

8 minutes read.

How to find Computer name on Windows?

How to find computer name on Windows The computer’s name is undoubtedly useful and needed at times to resolve technical issues or downloading any new software or pairing your system with...

4 minutes read.

How to Make Money Online?

How to make money online? Earning money from the traditional way of working is a way harder and requires a lot of manpower and capital. With the increase in the use of the Internet...

9 minutes read.

Does my computer have Bluetooth?

Does my computer have Bluetooth? With the advancement of IOT (Internet of things) and high-tech electronic gadgets, Bluetooth has become perennial in our digital lives. Most of us are already familiar...

6 minutes read.

How to Create Folders in Gmail

How to Create Folders in Gmail Gmail is a leading email service provider and is used by 1.5 billion-plus active users worldwide. All Gmail professional users receive at least dozens of...

6 minutes read.

What does RAM do on a computer?

What does RAM do on a computer? Many of us might have heard the word RAM in our daily life but do not know what it does except the fact that...

10 minutes read.

How to schedule an email in Gmail?

How to schedule an email in Gmail Imagine scenarios where you send an important message to one of your international clients, and you know the time difference is significant. Earlier, you...

7 minutes read.

How to write a cover letter

How to write a cover letter The letter that you include with your job application is known as a cover letter. One is suggested to always include a cover letter with your job application...

5 minutes read.

How to Multiply Fractions?

What is a Fraction? A fraction is a number that divides a whole number into equal parts. A fraction tells us how many parts we have from the whole part. We...

2 minutes read.

How to Create a Group Email in Gmail

How to Create a Group Email in Gmail Every now and then, one needs to pass on or share a message to several people at the same time. To make this...

6 minutes read.

How to cancel amazon prime?

How to cancel amazon prime? Amazon Prime is a subscription that offers variety of services with it. The advantage of amazon prime is that you can expect hassle-free shipping within two days, access to...

3 minutes read.

How to Find Connected Components in Graph

In this article, we are going to find connected components in a graph by building a program in C++ language. We are going to use different approaches to build the...

4 minutes read.

How to Check String is Alphanumeric or Not Using Regular Expression

If we have a string and want to know whether it is either alphanumeric or not, we can do it using Regular Expression(regex). What is Regular Expression? A Regular Expressionor regexis a...

3 minutes read.