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 build a Computer?

How to build a computer Computing technology has evolved a lot in the last decade and has come a long way since then. For many years building computers was as tough...

17 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.

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 add Signature to your Mail

How to add Signature to your Mail Gmail has formed an inevitable aura around the business world. Gmail was launched in the year 2004 as an email service provider with very...

7 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.

How To Connect Air pods To Computer?

How To Connect Air pods To Computer Apple has put light on a statement and said that it believes the future is wireless. This technology is pacing up with the time,...

5 minutes read.

How to delete Gmail Account?

What is Gmail? Gmail is a free mobile and web-based email service introduced by Google on April 1, 2004. Gmail is used for personal to business purposes. It allows users to...

2 minutes read.

How to backup iPhone to Computer?

How to backup iPhone to computer iPhone or iPad is an electronic gadget useful to store all preferences, memories, personal or official data, unlike essential documents, emails, messages, photos, running apps,...

6 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 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 compress jpeg file to 100kb?

Nowadays, everyone loves to share cool snapshots on social media handles. Before putting the images on any site, editing is an integral part. File compression is one of method by...

3 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 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 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.

How to send an email in Gmail?

How to send an email in Gmail Emails are extensively used to send or receive messages or documents online through computer or through Gmail app. In this tutorial, we will come...

6 minutes read.

How to write a cheque?

How to write a cheque? In today's time, digital payments are consistently gaining more market share, but it's also important to know how to write a cheque. Many companies, employers, workers, and landlords still...

2 minutes read.

How to take a screenshot on the iPhone?

How to take a screenshot on the iPhone? A screenshot is an excellent thing to quickly save your screen content, which is open on your screen. You can share your favorite videos, high scores...

4 minutes read.

How to flip your Computer Screen

How to flip your computer screen The word flip refers to turn over or rotate something quickly. Computers provide the flip functionality, which facilitates the providers to rotate the screen in...

5 minutes read.

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...

3 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.